如何解决Python PostgreSQL更新不更改行,但没有错误
尝试在Postgres 12数据库上执行UPDATE语句。 选择工作正常。 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
解决方法
哇!添加
HttpHeaders.ACCEPT_ENCODING,"deflate"
实例化db_conn对象后就成功了!
,不是答案,而是一种显示我所得到的方法:
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
一起使用。更改将显示在数据库中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。