我正在编写一个程序来存储信用卡值作为练习。 我不断收到错误“ sqlite3.OperationalError:没有这样的列: 将创建该表,然后创建一列:名称。 列名称存在于SQLiteStudio中cc.db的cards表中 任何帮助表示赞赏。
import sqlite3
conn = sqlite3.connect('cc.db')
c = conn.cursor()
def createTABLE():
c.execute("""CREATE TABLE IF NOT EXISTS cards (
name text,
ccnumber integer,
exp_date text,
csv integer
)""")
conn.commit()
print('table created')
def entercard():
ccname = input('Enter the name of the new card: ')
ccnumber = input('Enter the card number: ')
ccexp_date = input('Enter the Expiration date: ')
cccsv = input('Enter the CSV number from the back of the card: ')
c.execute("INSERT INTO cards VALUES (?, ?, ?, ?),(name, ccnumber, exp_date, csv)");
conn.commit()
def printall():
for card in c.execute('SELECT * FROM cards'):
print(card)
createTABLE()
entercard()
printall()
conn.close()
答案 0 :(得分:1)
我无法确定您为什么会遇到该特定错误,但是您对以下行有疑问:
c.execute("INSERT INTO cards VALUES (?, ?, ?, ?),(name, ccnumber, exp_date, csv)");
都是字符串。您需要像这样将变量与查询字符串分开:
c.execute("INSERT INTO cards VALUES (?, ?, ?, ?)",(name, ccnumber, exp_date, csv))
答案 1 :(得分:-1)
我做了下面的事情,以便存储值并从表中检索它们。 您需要使该行看起来像这样。
c.execute("INSERT INTO cards(name,ccnumber,exp_date,csv) VALUES ('Tom','new1','new2','new3');")
空格
import sqlite3
conn = sqlite3.connect('cc.db')
c = conn.cursor()
def createTABLE():
c.execute("""CREATE TABLE IF NOT EXISTS cards (
name text,
ccnumber integer,
exp_date text,
csv integer)""")
conn.commit()
c.execute("INSERT INTO cards(name) VALUES ('Tom');")
conn.commit()
p=c.execute('SELECT * FROM cards')
j= p.fetchall()
print(j)
for i in j:
print(i)
print(p)
print('table created')
def entercard():
ccname = input('Enter the name of the new card: ')
c.execute("INSERT INTO cards(name) VALUES ('"+ccname+"')")
conn.commit()
def printall():
for card in c.execute('SELECT * FROM cards'):
print(card)
createTABLE()
entercard()
printall()
conn.close()