Python SQLAlchemy中的更新出错
我有一段代码(python 3.6、SQLAlchemy 1.1.6)有问题,这段代码运行良好:Python SQLAlchemy中的更新出错,python,sqlalchemy,Python,Sqlalchemy,我有一段代码(python 3.6、SQLAlchemy 1.1.6)有问题,这段代码运行良好: def delete_item(): input_id = int(input('Select ID number of item to delete: ')) delete = create_table().delete().where(create_table().c.id == input_id) # Commit delete connection.exe
def delete_item():
input_id = int(input('Select ID number of item to delete: '))
delete = create_table().delete().where(create_table().c.id == input_id)
# Commit delete
connection.execute(delete)
但这一次我犯了一个错误,但我真的不知道为什么:
def update_item():
input_id = int(input('Select ID number of item to change: '))
# Get data from rows with selected ID number
select_data = select(['*']).where(
create_table().c.id == input_id)
for row in connection.execute(select_data):
input_name = input('New name for name: {}: '.format(row[1]))
input_description = input(
'New description for description: {}: '.format(row[6]))
input_exclusion = input(
'New exclusions for exclusion: {}: '.format(row[7]))
# OperationalError
update_data = update(create_table()).where(create_table().c.id == input_id).values(
name='{}'.format(input_name),
description='{}'.format(input_description),
exclusion='{}'.format(input_exclusion))
# Commit change
connection.execute(update_data)
回溯消息:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "FROM":
syntax error [SQL: 'UPDATE clothes_data SET name=?, description=?,
exclusion=? FROM clothes_data WHERE clothes_data.id = ?'] [parameters:
('new name', 'new desc', 'new excl', 92)]
创建表格函数:
def create_table():
metadata = MetaData()
# set name of table, names of columns, kind of data in columns
clothes_data = Table('clothes_data', metadata,
#columns
)
# commit changes in data base
metadata.create_all(engine)
return clothes_data
问题的根源在于创建表的方式。由于
create_table()
每次调用时都会创建新的元数据和一个新的table
实例,因此SQLAlchemy将它们视为不同的实体。通常,您应该为每个程序创建一次表定义
所以在
WHERE子句中的表不是要更新的表,因此您触发了SQLite不支持的。修复将取决于您如何设置程序的结构,但您可以创建一个名为model
的模块,在其中存储表和声明性类。一个快速且脏的修复将是
def update_item():
input_id = int(input('Select ID number of item to change: '))
select_data = select(['*']).where(create_table().c.id == input_id)
for row in connection.execute(select_data):
input_name = input('New name for name: {}: '.format(row[1]))
input_description = input(
'New description for description: {}: '.format(row[6]))
input_exclusion = input(
'New exclusions for exclusion: {}: '.format(row[7]))
# CREATE ONCE
table = create_table()
update_data = update(table).where(table.c.id == input_id).values(
name='{}'.format(input_name),
description='{}'.format(input_description),
exclusion='{}'.format(input_exclusion))
# Commits changes, IF autocommit is in use
connection.execute(update_data)
但是请将您的表和模型类移动到一个模块。应该已经发布了错误消息-create\u table()
函数是什么样子的?我怀疑它在每次调用时都会创建表对象的新实例,这反过来会导致update()
生成不正确的查询。@MarcinKoprek,更新您的问题正文,而不是将代码作为注释发布
def update_item():
input_id = int(input('Select ID number of item to change: '))
select_data = select(['*']).where(create_table().c.id == input_id)
for row in connection.execute(select_data):
input_name = input('New name for name: {}: '.format(row[1]))
input_description = input(
'New description for description: {}: '.format(row[6]))
input_exclusion = input(
'New exclusions for exclusion: {}: '.format(row[7]))
# CREATE ONCE
table = create_table()
update_data = update(table).where(table.c.id == input_id).values(
name='{}'.format(input_name),
description='{}'.format(input_description),
exclusion='{}'.format(input_exclusion))
# Commits changes, IF autocommit is in use
connection.execute(update_data)