我一直在嘗試更新 sqlite3 表中的一些值,但是我遇到了很多與 `sqlite3.OperationalError: near "GROUP": 語法錯誤有關的問題。我一直在關注檔案,但糾正錯誤并不是很成功。我要做的是找到值 = 0 并使用 max(value) 1 更新的 all_numbers 我只需要說我是新創建的資料庫,因此我們將不勝感激。提前致謝。
> def update_tables(connection): #function to update table
> c = connection.cursor() # i have a separated def to create the connection
> data = """SELECT id, cps, all_numb FROM all_records""" #this is my column selection
> c.execute(data)
> rows = (c.fetchall())
> # print(rows)
> _list = []
> for row in rows: # I try to use a python loop to iterate through the rows
> id = row[0]
> cps = row[1]
> all_numb = row[2]
> if cps not in _list:
> _list.append(cps)
> if cps in _list: #here I tried a for loop but it does not work
> sq3 = """UPDATE all_records # this is the update selection
> SET all_numb =(SELECT MAX(all_numb) 1)
> WHERE cps =cps AND all_numb = 0
> GROUP BY cps"""
> c.execute(sq3)
> connection.commit()
> c.fetchall()
uj5u.com熱心網友回復:
我認為這就是你想要做的:
UPDATE all_records AS r1
SET all_numb = last_numb 1
FROM (
SELECT r2.cps, MAX(r2.all_numb) AS last_numb
FROM all_records AS r2
GROUP BY r2.cps
)
WHERE r1.cps = r2.cps AND all_numb = 0
參見檔案UPDATE FROM
我不確定您為什么要在for回圈中執行此操作,因為您沒有使用回圈中的任何變數。這會立即更新整個表,因此不需要回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422602.html
標籤:
上一篇:sqlite'=':無法從'constchar[164]'轉換為'char*'
下一篇:SQLite中行的不同計算
