Python sqlite - 引用execute()方法中的用法

时间:2015-12-27 11:34:14

标签: python python-2.7 sqlite

在阅读python sqlite3 DB-API时,创建表格的第一个示例使用单引号:

c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

但在其他例子中有双引号:

cur.execute("create table people (name_last, age)")

所以我有2个问题:

  1. 这真的有什么不同吗?它如何影响create table命令?
  2. 哪一个更适合使用参数?例如:

    c.execute('''CREATE TABLE %s(date text, trans text, symbol text, qty real, price real)''' % table_name)

  3. VS

     cur.execute("create table %s (name_last, age)" % table_name)
    

    由于

3 个答案:

答案 0 :(得分:3)

基本上没有双引号或单引号的规则。

但连续三个引号开始多行引用值。

答案 1 :(得分:1)

使用三个"""就像评论一行一样

#this text python doesn't read so you can write about your code

"""it allows
you to type
over multiple
lines."""

此外,只要您使用相同的样式关闭,您就可以使用"' python查看它们。

答案 2 :(得分:1)

Is this really any difference? How can it affect create table command?

使用单引号或双引号字符串或三引号字符串不会以任何方式影响create命令。它们最终只是create命令的字符串参数。

Which one is better to use with parameters?

双引号是大多数python程序的常态。三重引号主要用于从其他地方复制粘贴的大型多行字符串,并且您希望将它们嵌入到程序中。在上面给出的链接中,we can see one such example在cursor.executescript()中的用法,其中sql脚本(之前可能存在)在程序中使用。