【发布时间】:2022-07-11 22:03:40
【问题描述】:
大家好,我正在学习 sql-lite,在更新表格时出现错误 UNIQUE 约束失败:contacts.Firstname 如何正确处理更新方法列具有唯一性
import sqlite3
try:
conn = sqlite3.connect("database.db")
except Exception as e:
raise e
cursor = conn.cursor()
sql_command = """
CREATE TABLE IF NOT EXISTS contacts (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Firstname TEXT Unique,
Lastname TEXT,
Email TEXT
)"""
cursor.execute(sql_command)
insert_data = """
INSERT INTO contacts
(Firstname, Lastname, Email)
VALUES (
'David',
'Attenborough',
'dattenboroughm@example.com'
)
"""
cursor.execute(insert_data)
# Commit the changes to the database
conn.commit()
cursor.execute('UPDATE contacts SET Firstname = "pP" WHERE Id = 2')
# Commit the changes to the database
conn.commit()
conn.close()
【问题讨论】:
标签: python mysql python-3.x sqlite