sqlite3数据输入错误

时间:2015-10-23 08:17:30

标签: python sqlite

使用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))

1 个答案:

答案 0 :(得分:-1)

我认为您在查询中忘记了'

Where Name = '{1}'