我正在尝试使用SQL创建一个表并输入一些值
import sqlite3
conn = sqlite3.connect("Classes.db")
c = conn.cursor()
score1 = 5
score2 = 5
score3 = 5
name = ("Brad")
Class = 2
def tableCreate():
c.execute(("CREATE TABLE Class{}(Name TEXT, Score1 INT,Score2 INT, Score3 INT)").format(Class))
def dataEntry():
c.execute(("INSERT INTO Class{} (Name,Score1,Score2,Score3) VALUES (?,?,?,?)").format(Class),
(name,score1,score2,score3).format(Class))
conn.commit()
运行dataEntry()
时遇到此错误line 13, in dataEntry
(name,score1,score2,score3).format(Class))
AttributeError: 'tuple' object has no attribute 'format'
答案 0 :(得分:0)
您只需要在查询字符串上调用格式,而不是参数元组:
c.execute(
"INSERT INTO Class{} (Name,Score1,Score2,Score3) VALUES (?,?,?,?)".format(Class),
(name,score1,score2,score3))
也许字符串周围的括号让你感到困惑;那些是多余的,我也删除了它们。