Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite - Use backticks (`) or double quotes (") with python

I saw a similar question in 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 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.).

like image 879
dylnmc Avatar asked Aug 05 '14 14:08

dylnmc


People also ask

Does SQLite Use single or double quotes?

Double-quoted String Literals Are Accepted The SQL standard requires double-quotes around identifiers and single-quotes around string literals. For example: "this is a legal SQL column name" 'this is an SQL string literal' SQLite accepts both of the above.

Is it better to use double or single quotes in Python?

Use single-quotes for string literals, e.g. 'my-identifier' , but use double-quotes for strings that are likely to contain single-quote characters as part of the string itself (such as error messages, or any strings containing natural language), e.g. "You've got an error!" .

Can you use double quotes in Python?

Python does have two simple ways to put quote symbols in strings. You are allowed to start and end a string literal with single quotes (also known as apostrophes), like 'blah blah' . Then, double quotes can go in between, such as 'I said "Wow!" to him. '

What is the difference between single and double quotation marks in SQL?

Double quotes are used to indicate identifiers within the database, which are objects like tables, column names, and roles. In contrast, single quotes are used to indicate string literals.


2 Answers

The SQL standard says that strings must use 'single quotes', and identifiers (such as table and column names), when quoted, must use "double quotes".

For compatibility with MySQL, SQLite also allows to use single quotes for identifiers and double quotes for strings, but only when the context makes the meaning unambiguous. (In SELECT 'rowid' ..., a string is allowed, so a string is what you get.) If possible, always use the standard SQL quotes.

For compatibility with MySQL, SQLite also allows `backticks` for identifiers.

For compatibility with Microsft databases, SQLite also allows [brackets] for identifiers.

(This works in all SQLite versions.)

like image 135
CL. Avatar answered Sep 22 '22 18:09

CL.


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: http://www.sqlite.org/lang_keywords.html

like image 40
laalto Avatar answered Sep 23 '22 18:09

laalto