python sqlite - 更新 cmd

问题描述 投票:0回答:1

我正在尝试使用 python 更新 db (sqlite3) 字段之一,但它不起作用。

def update(self, old_name, new_name):
    conn = sqlite3.connect(os.path.join(self._path, "files.db"))
    cursor = conn.cursor()  # Create a cursor

    try:
        query = f"UPDATE files SET filename ={new_name} WHERE filename = {old_name};"
        cursor.execute(query)

    except Exception as e:
        return e

    else:
        return True

    conn.close()

该代码应该更改文件名。

python database sqlite
1个回答
0
投票

执行查询后调用

conn.commit()

def update(self, old_name, new_name):
    conn = sqlite3.connect(os.path.join(self._path, "files.db"))
    cursor = conn.cursor()  # Create a cursor


    try:
        query = f"UPDATE files SET filename ={new_name} WHERE filename = {old_name};"
        cursor.execute(query)
        conn.commit()


    except Exception as e:
        return e

    else:
        return True

    conn.close()
© www.soinside.com 2019 - 2024. All rights reserved.