2017-07-06 59 views
0

这是我的代码:Python的sqlite3的和字符串格式化 - 错误

title = "Importunate Widow" 
conn = sqlite3.connect('parable.sqlite') 
c = conn.cursor() 
sqlite3.enable_callback_tracebacks(True) 

c.executescript(""" 
      CREATE TABLE IF NOT EXISTS _index(
       title text NOT NULL, 
       context text NOT NULL 
       ); 

      CREATE TABLE IF NOT EXISTS {}(
       value int NOT NULL, 
       number int NOT NULL, 
       question text NOT NULL, 
       choice1 text, 
       choice2 text, 
       choice3 text, 
       choice4 text, 
       answer text NOT NULL, 
       explanation text, 
       see text 
       )""".format(title)) 

    c.executemany('INSERT INTO _index(title,context) VALUES(?,?)', index) 

    c.executemany('INSERT INTO {}(value,number,question,choice1,choice2,choice3,choice4,answer,explanation,see) VALUES(?,?,?,?,?,?,?,?,?,?)'.format(title), quiz) 
    conn.commit() 
    conn.close() 

这给了我:

OperationalError Traceback (most recent call last) 
/home/user/Hobby/Quiz/quiz_sqlite.py in <module>() 
sqlite3.enable_callback_tracebacks(True) 
    87 
---> 88   c.executescript("""CREATE TABLE IF NOT EXISTS _index(title text NOT NULL,context text NOT NULL);CREATE TABLE IF NOT EXISTS {}(value int NOT NULL,number int NOT NULL,question text NOT NULL,choice1 text,choice2 text,choice3 text,choice4 text,answer text NOT NULL,explanation text,check text)""".format(title)) 
    89 
    90   c.executemany('INSERT INTO _index(title,context) VALUES(?,?)', index) 

OperationalError: near "Widow": syntax error 

我基本上遍历多个文件,并插入数据文件后,命名表。我搜索了互联网,并没有出现任何东西。

我的尝试:

  1. 试过之后see text
  2. 试图改变列的名称seerefer

无添加;)"""

  • 尝试添加,后这有助于!

    代码中的语法错误是什么?

  • 回答

    1

    您试图使用Importunate Widow作为表名称。您不允许在表格或字段名称中使用空格。