簡體   English   中英

Python sqlite3更新語句未更新

[英]Python sqlite3 update statment not updating

所以我有了這個sql(它使用(1,1,1),因為我想消除它為OrderID輸入錯誤參數的可能性(在這種情況下為1)),它使用的是顯示如下。 由於某種原因,它在運行時不會返回錯誤,但是即使它看起來與我的代碼中其他地方的sql相同,也不會更新該表。

我數據庫中的所有字段均設置為除ID之外的文本...我想就是全部,在此先感謝您的建議。

query("UPDATE Orders SET CustomerID = ?, OrderDate = ? WHERE OrderID = ?", (1, 1, 1))

def query(sql, data = None):
with sqlite3.connect("notDataBase1.db") as db:
    cursor = db.cursor()
    if data == None:
        cursor.execute(sql)
    else:
        cursor.execute(sql, data)
    results = cursor.fetchall()
    return results

就像有人在評論中說的那樣,您需要在查詢后運行db.commit()才能將事務寫入數據庫。 您可以閱讀文檔以了解更多詳細信息。 您也不需要獲取UPDATE查詢。 像這樣:

update("UPDATE Orders SET CustomerID = ?, OrderDate = ? WHERE OrderID = ?", (1, 1, 1))

def update(sql, data):
    with sqlite3.connect("notDataBase1.db") as db:
        cursor = db.cursor()
        cursor.execute(sql, data)
        db.commit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM