我正在尝试制作一个简单的售票电影院,我正在复制这个特定的 YouTuber 作品以供参考,但我一直遇到错误,它说的是这个(看图片)。我不知道这里有什么不匹配,因为我已经检查了多次,这是代码:
import sqlite3
def create_table():
conn = sqlite3.connect('Reservation.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Tickets (
ticket_id INTEGER PRIMARY KEY,
movie_name TEXT,
ticket_quantity INTEGER,
ticket_price INTEGER
)''')
conn.commit()
conn.close()
def insert_Tickets():
conn = sqlite3.connect('Reservation.db')
cursor = conn.cursor()
Tickets_data = [
('T1', 'Movie1', 3, 50),
('T2', 'Movie2', 2, 40),
('T3', 'Movie3', 4, 60),
('T4', 'Movie4', 5, 60),
('T5', 'Movie5', 1, 65)
]
cursor.executemany('INSERT OR REPLACE INTO Tickets (ticket_id, movie_name, ticket_quantity, ticket_price) VALUES (?, ?, ?, ?)', Tickets_data)
conn.commit()
conn.close()
def get_tickets():
conn = sqlite3.connect('Reservation.db')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Tickets')
tickets = cursor.fetchall()
conn.close()
return tickets
def update_quantity(id,reserved_quantity):
conn = sqlite3.connect('Reservation.db')
cursor = conn.cursor()
cursor.execute('UPDATE Tickets SET ticket_quantity = ticket_quantity - ? WHERE ticket_id = ?',(reserved_quantity,id))
conn.commit()
conn.close()
create_table()
insert_Tickets()
如果为主键添加一列,它会起作用: 将您的创建表更改为:
cursor.execute('''
CREATE TABLE IF NOT EXISTS Tickets (
id INTEGER NOT NULL PRIMARY KEY,
ticket_id Text,
movie_name TEXT,
ticket_quantity INTEGER,
ticket_price INTEGER
)''')