[英]Python postgreSQL update not changing row but no error
Trying to execute an UPDATE statement on a Postgres 12 database.尝试在 Postgres 12 数据库上执行更新语句。 SELECTs work fine.
选择工作正常。 db user has full read/write permissions.
db 用户具有完全的读/写权限。 Can't get Python to display/log any db errors.
无法获取 Python 来显示/记录任何数据库错误。 Removed "TRY... EXCEPT" from the code below for easier readability.
为了便于阅读,从下面的代码中删除了“TRY...EXCEPT”。 The Python program flow is uninterrupted, as if the update worked.
Python程序流程不间断,好像更新成功了。 But I check the database and the i_sec_level did not change to 5.
但是我检查了数据库,i_sec_level 并没有变成 5。
Relevant Python code:相关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()
Python log of db_cursor.query: db_cursor.query 的 Python 日志:
UPDATE tbl_users SET i_sec_level=5 WHERE id = 3
Postgres 12 log: 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
Woohoo!哇哦! Adding
添加
db_conn.autocommit=True
after instantiating the db_conn object did the trick!在实例化 db_conn object 之后成功了!
Not an answer, but a way to show what I get:不是答案,而是一种展示我得到的东西的方法:
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
This is with psycopg2-binary==2.8.6
and Postgres 12.3
.这是
psycopg2-binary==2.8.6
和Postgres 12.3
。 The change shows up in the database.更改显示在数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.