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



在阅读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)")

所以我有两个问题:

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

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

与。

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

感谢

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

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

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

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

Which one is better to use with parameters?

在大多数python程序中,双引号是常态。当你从其他地方粘贴了一个大的多行字符串副本,并且你想把它们嵌入到你的程序中时,通常会使用三引号。在您上面给出的链接中,我们可以在cursor.executescript()中的用法中看到一个这样的例子,其中在程序中使用了一个sql脚本(可能以前存在)。

使用三个"""就像注释掉一行

#this text python doesn't read so you can write about your code
"""it allows
you to type
over multiple
lines."""

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

最新更新