为什么我收到错误,没有这样的列名

我正在编写一个程序来存储信用卡值作为练习。 我不断收到错误“ 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()
qwe8530748 回答:为什么我收到错误,没有这样的列名

我无法确定您为什么会遇到该特定错误,但是您对以下行有疑问:

c.execute("INSERT INTO cards VALUES (?,?,?),(name,ccnumber,exp_date,csv)");

都是字符串。您需要像这样将变量与查询字符串分开:

c.execute("INSERT INTO cards VALUES (?,?)",csv))
,

我做了下面的事情,以便存储值并从表中检索它们。 您需要使该行看起来像这样。

    c.execute("INSERT INTO cards(name,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()
本文链接:https://www.f2er.com/3135597.html

大家都在问