SQLAlchemy中的更新出错

时间:2017-03-25 19:30:56

标签: 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.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

1 个答案:

答案 0 :(得分:1)

问题的根源是您创建表格的方式。由于create_table()每次调用它时都会创建新的元数据和新的Table实例,因此SQLAlchemy会将它们视为不同的实体。通常,您应该为每个程序创建一次表定义。

所以在

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))

WHERE子句中的表不是UPDATE的表,因此您触发了multiple table update,SQLite不支持。修复程序将取决于您如何设置程序的结构,但您可以创建一个名为model的模块,您可以在其中存储Table和声明性类。快速修复

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)

但请将您的表格和模型类移动到模块。