'Use backticks (`) or double quotes (") with Python and SQLite
I saw a similar question on Stack Overflow pertaining to Android, but I was wondering whether I should use backticks (`) or double quotes (") - using Python - to select table names or rowid or what have you.
I tried single quotes - like this select 'rowid', * from 'tbl' order by 'rowid'. The single quotes worked in some cases but not all. I learned to use double quotes or backticks, and I was looking at the SQLite database browser and I noticed that it used backticks.
I really like to put double quotes around my strings in Python, because I'm coming from Java, so it is natural to do cursor.execute("select 'rowid',* from 'table';"), and it would be just as easy to do backticks (the double quotes would require a backslash and make the query look a little confusing).
However, I just wanted to make sure that the backticks are portable (all versions of Windows, Linux, OS X, etc.).
Solution 1:[1]
Prefer double quotes for quoting identifiers, such as column or table names. It's the SQL standard.
Backticks also work, but they're only supported for MySQL syntax compatibility.
Single quotes are for string literals, not identifiers. That's why you'll get the literal value when using them.
Further reading: SQLite Keywords
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Peter Mortensen |
