SQLite table and column name requirements
If you use brackets or quotes you can use any name and there is no restriction :
create table [--This is a_valid.table+name!?] (x int);
But table names that don't have brackets around them should be any alphanumeric combination that doesn't start with a digit and does not contain any spaces.
You can use underline and $ but you can not use symbols like: + - ? ! * @ % ^ & # = / \ : " '
Sqlite3 Python: can't use limit as column name
"limit" is an SQL keyword, for example, as in
SELECT foo
FROM bar
LIMIT 10;
If you want to use "limit" as a column name in sqlite, it needs to be quoted, in one of these ways:
- 'limit'
- "limit"
- [limit]
- `limit`
So for example, your statement could be
cur.execute("""CREATE TABLE categories (
priority text,
name text,
type text,
increment text,
total real,
"limit" real)""")
Note that it must be quoted in other statements too, for example
"""INSERT INTO categories ("limit") VALUES (?);"""
What are valid table names in SQLite?
I haven't found a reference for it, but table names that are valid without using brackets around them should be any alphanumeric combination that doesn't start with a digit:
abc123 - valid
123abc - not valid
abc_123 - valid
_123abc - valid
abc-abc - not valid (looks like an expression)
abc.abc - not valid (looks like a database.table notation)
With brackets you should be able to use pretty much anything as a table name:
[This should-be a_valid.table+name!?]
SQLite column in table has same name as table
This is not about the table/column name: having a table column with the same name as the table it belongs to is unambiguous in SQL, since the syntax is clear enough about where a column name or a table name is expected.
Table country
is not part of the from
clause of your query, hence it is not available in the select
clause.
You need to join
that table, presumably with the incountry
table:
inner join country on country.cid = incountry.cid
Then you can add country.country
to the select
clause.
SQLite, LIKE column name
You can use pragma_table_info()
with the table's name:
SELECT name
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
You can use this query:
SELECT GROUP_CONCAT(name) AS columns
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
which returns only 1 column columns
with a string value like 'thing_column1,thing_column2,thing_column3'
and you can use it to construct a SELECT
statement in your application.
Related Topics
SQL Statement Help - Select Latest Order for Each Customer
How to Rename an Index in MySQL
SQL Server 2005 Using Dateadd to Add a Day to a Date
Query Across Multiple Databases on Same Server
How to Write SQL Using Speech Recognition
Return Bit Value as 1/0 and Not True/False in SQL Server
Select Distinct on One Column, Return Multiple Other Columns (SQL Server)
Best-Performance Query for "Select Max in Group"
How to Select the Most Frequently Appearing Values
Arithmetic Overflow Error Converting Numeric to Data Type Numeric
Extracting the Total Number of Seconds from an Interval Data-Type
How to Select Columns from a Table Which Have Non Null Values
Delphi - Prevent Against SQL Injection
Windowed Functions Can Only Appear in the Select or Order by Clauses
How to Pull a List of Id's from a SQL Table as a Comma-Separated Values String
Ole Db Provider 'Microsoft.Jet.Oledb.4.0' Cannot Be Used for Distributed Queries