0
更新我有一個代碼(蟒蛇3.6,SQLAlchemy的1.1.6)的一個問題,這個工作得很好:錯誤與SQLAlchemy的
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)]
CREATE_TABLE功能:
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()'函數是什麼樣的?我懷疑它會在每次調用時創建表對象的新實例,從而導致'update()'產生不正確的查詢。 –
@MarcinKoprek,更新您的問題主體而不是將您的代碼發佈爲評論 –