Sqlite-在Python中使用反引号(`)或双引号(') [英] Sqlite - Use backticks (`) or double quotes (") with python

查看:366
本文介绍了Sqlite-在Python中使用反引号(`)或双引号(')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Stack Overflow中看到了一个与Android有关的类似问题,但我想知道是否应该使用反引号(`)或双引号()-使用Python-选择表名或rowid或您拥有什么。



我尝试了单引号-像这样选择'rowid',*从'tbl'顺序按'rowid'。单引号在某些情况下有效,但不是全部。我学会了使用双引号或反引号,而我在查看 SQLite数据库浏览器时,我注意到它使用了反引号。



我真的很想在Python中用双引号引起来,因为我来自Java,因此很自然地执行 cursor.execute(选择'rowid',* from'table';),反引号也很容易(双引号需要反斜杠,并使查询看起来有些混乱)。 ,我只是想确保反引号是可移植的(Windows,Linux,Os x等所有版本)。

解决方案

SQL标准规定,字符串必须使用的单引号,并且标识符(例如表名和列名)在引用时必须使用双引号



为了与MySQL兼容,SQLite也允许使用标识符的单引号和字符串的双引号,但前提是上下文使含义明确。 (在 SELECT'rowid'... 中,允许使用字符串,因此您将得到一个字符串。)
如果可能,请始终使用标准SQL引号



为了与MySQL兼容,SQLite还允许`backticks` 作为标识符。



为了与Microsft数据库兼容,SQLite还允许 [括号] 作为标识符。



(这在所有SQLite版本中都适用。)


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.).

解决方案

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.)

这篇关于Sqlite-在Python中使用反引号(`)或双引号(')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆