简体   繁体   中英

Error encountered while using the SQL Module

I am attempting to create a table using SQL and enter some values

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()

I encounter this error when running dataEntry()

line 13, in dataEntry
    (name,score1,score2,score3).format(Class))
AttributeError: 'tuple' object has no attribute 'format'

You need to call format only on the query string , not on the parameters tuple:

c.execute(
    "INSERT INTO Class{} (Name,Score1,Score2,Score3) VALUES (?,?,?,?)".format(Class),
    (name,score1,score2,score3))

Perhaps the parentheses around the string confused you; those were redundant and I removed them too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM