2015-10-23 79 views
0

製作與sqlite3的算術測驗和不斷收到此錯誤信息:sqlite3的數據輸入錯誤

Traceback (most recent call last): 
    File "/Volumes/2011$/computing/computing/arithmetic quiz/StupidSyntax.py", line 121, in <module> 
    cur.execute('INSERT INTO ' + table + ' (Surname, Name, Score) VALUES (?, ?, ?)', (last_name, name, result)) 
sqlite3.InterfaceError: Error binding parameter 2 - probably unsupported type. 

我一直在努力,現在解決這個問題的幾天,我無法找到的問題它。

下面的代碼,我是新來的python-sqlite3的:

import sqlite3 as lite 
import sys 
import random 
import operator 
import os 

OPERATIONS = [ 
    (operator.add, "+"), 
    (operator.mul, "*"), 
    (operator.sub, "-") 
    ] 

NB_QUESTIONS = 10 
db = "class_database.db" 
db_exists = None 

if os.path.isfile(db): 
    db_exists = True 
    con = lite.connect(db) 
else: 
    db_exists = False 
    con = lite.connect(db) 
    for i in range(1, 4): 
     con.execute("CREATE TABLE class%s (Surname TEXT, Name TEXT, Score INTEGER) ; " %i) 

print() 
if db_exists == True: 
    print("Database found! Loading now...") 
else: 
    print("New database created") 

def get_int_input(prompt=''): 
    while True: 
     try: 
     return int(input(prompt)) 
     except ValueError: 
     print("Not a valid input (integer is expected)") 

def get_bool_input(prompt=''): 
    while True: 
     val = input(prompt).lower() 
     if val == 'yes': 
      return True 
     elif val == 'no': 
      return False 
     else: 
      print("Not a valid input (yes/no is expected)") 

if __name__ == '__main__': 
    name = "" 
    while name != name.isalpha(): 
     name = input("What is your first name? ").title() 
     if name.isalpha(): 
      break 

    last_name = "" 
    while last_name != last_name.isalpha(): 
     last_name = input("What is your last name? ").title() 
     if last_name.isalpha(): 
      break 

    class_name = "" 
    while class_name != class_name.isdigit(): 
     class_name = input("Which class do you wish to input results for? 1/2/3 ") 
     if class_name.isdigit(): 
      break 

    print(name, last_name, ", Welcome to the OCR Controlled Assessment Maths Test") 

    score = 0 
    for _ in range(NB_QUESTIONS): 
     num1 = random.randint(1,12) 
     num2 = random.randint(1,12) 
     op, symbol = random.choice(OPERATIONS) 
     print("What is", num1, symbol, num2) 
     if get_int_input() == op(num1, num2): 
      print("Correct") 
      score += 1 
     else: 
      print("Incorrect") 

    print("Well done", name, "you scored", score, "/", NB_QUESTIONS) 
    result = (score, "/", NB_QUESTIONS) 

    with con: 
     table = "class%s" %(class_name) 
     cur = con.cursor() 
     cur.execute('SELECT name FROM sqlite_master where (name = ?)', [table]) 
     row = cur.fetchall() 
     valid_table = False 
     if row: 
      valid_table = True 
     if valid_table: 
      cur = con.cursor() 
      cur.execute('SELECT * from {0} WHERE Name = {1} ;'.format(table, name)) 
      row = cur.fetchall() 
      if len(row)>0: 
       cur.execute('UPDATE ' + table + ' WHERE Surname = "%s", WHERE Name = "%s", SET Score = "%s";'% (last_name, name, result)) 
       print ("Your score has been updated successfully!") 
      else: 
       cur.execute('INSERT INTO ' + table + ' (Surname, Name, Score) VALUES (?, ?, ?)', (last_name, name, result)) 
       print("You have been added to %s and your score has been recorded.\n" %(table)) 
+0

而不是創建4個表,可以考慮創建只有一個具有用於指定類ID的列的類表。動態構建表名通常是一個壞主意。還要確保你正在使用查詢參數;不要在查詢字符串中使用'format'。 –

回答

-1

我想你已經忘記在您的查詢'

Where Name = '{1}'