![](/img/trans.png)
[英]UPSERT syntax error linked to UPDATE in PostgreSQL (python)
[英]Python postgreSQL update not changing row but no error
嘗試在 Postgres 12 數據庫上執行更新語句。 選擇工作正常。 db 用戶具有完全的讀/寫權限。 無法獲取 Python 來顯示/記錄任何數據庫錯誤。 為了便於閱讀,從下面的代碼中刪除了“TRY...EXCEPT”。 Python程序流程不間斷,好像更新成功了。 但是我檢查了數據庫,i_sec_level 並沒有變成 5。
相關Python代碼:
db_conn = psycopg2.connect(
host=t_host,
port=t_port,
dbname=t_dbname,
user=t_user,
password=t_pw,
cursor_factory=DictCursor
)
db_cursor = db_conn.cursor()
q = ""
q += "UPDATE tbl_users SET "
q += " i_security_level = %(i_security_level)s"
q += " WHERE "
q += " id = %(id_user)s"
vars = {
'i_security_level': int(i_security_level),
'id_user': int(id_user)
}
db_cursor.execute(q,vars)
logging.debug(db_cursor.query)
db_conn.commit()
db_cursor.close()
db_conn.close()
db_cursor.query 的 Python 日志:
UPDATE tbl_users SET i_sec_level=5 WHERE id = 3
Postgres 12 日志:
2020-09-22 14:33:47.112 CDT [640] LOG: statement: UPDATE tbl_users SET i_sec_level=5 WHERE id = 3
2020-09-22 14:33:47.194 CDT [3284] DEBUG: shmem_exit(0): 1 before_shmem_exit callbacks to make
2020-09-22 14:33:47.195 CDT [3284] DEBUG: shmem_exit(0): 6 on_shmem_exit callbacks to make
2020-09-22 14:33:47.197 CDT [3284] DEBUG: proc_exit(0): 3 callbacks to make
2020-09-22 14:33:47.197 CDT [3284] DEBUG: exit(0)
2020-09-22 14:33:47.199 CDT [3284] DEBUG: shmem_exit(-1): 0 before_shmem_exit callbacks to make
2020-09-22 14:33:47.200 CDT [3284] DEBUG: shmem_exit(-1): 0 on_shmem_exit callbacks to make
2020-09-22 14:33:47.201 CDT [3284] DEBUG: proc_exit(-1): 0 callbacks to make
2020-09-22 14:33:47.208 CDT [12120] DEBUG: forked new backend, pid=8656 socket=5320
2020-09-22 14:33:47.209 CDT [12120] DEBUG: reaping dead processes
2020-09-22 14:33:47.211 CDT [12120] DEBUG: server process (PID 3284) exited with exit code 0
2020-09-22 14:33:47.220 CDT [12144] DEBUG: snapshot of 1+0 running transaction ids (lsn 0/17DE0B0 oldest xid 596 latest complete 595 next xid 597)
2020-09-22 14:33:47.250 CDT [8656] DEBUG: postgres child[8656]: starting with (
2020-09-22 14:33:47.261 CDT [8656] DEBUG: postgres
2020-09-22 14:33:47.262 CDT [8656] DEBUG: )
2020-09-22 14:33:47.262 CDT [8656] DEBUG: InitPostgres
2020-09-22 14:33:47.263 CDT [8656] DEBUG: my backend ID is 4
2020-09-22 14:33:47.265 CDT [8656] DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
2020-09-22 14:33:47.267 CDT [8656] DEBUG: received password packet
2020-09-22 14:33:47.272 CDT [8656] DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
哇哦! 添加
db_conn.autocommit=True
在實例化 db_conn object 之后成功了!
不是答案,而是一種展示我得到的東西的方法:
import psycopg2
con = psycopg2.connect("dbname=production host=localhost user=postgres")
cur = con.cursor()
cur.execute("update cell_per set cell_per = %s where category= 'HERB 3.5'", (19,))
con.commit()
# Postgres log at debug5
[unknown]-postgres-2020-09-22 17:23:17.237 PDT-0DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
[unknown]-postgres-2020-09-22 17:23:17.238 PDT-0LOG: statement: update cell_per set cell_per = 19 where category= 'HERB 3.5'
--2020-09-22 17:23:18.223 PDT-0DEBUG: snapshot of 1+0 running transaction ids (lsn 1/55611B50 oldest xid 133938 latest complete 133937 next xid 133939)
[unknown]-postgres-2020-09-22 17:23:18.246 PDT-133938DEBUG: CommitTransaction(1) name: unnamed; blockState: END; state: INPROGRESS, xid/subid/cid: 133938/1/1
這是psycopg2-binary==2.8.6
和Postgres 12.3
。 更改顯示在數據庫中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.