2017-07-06 26 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作爲表名稱。您不允許在表格或字段名稱中使用空格。