Ria*_*Ria 3 python database postgresql
entrym='entry'
entrym=entrym+ str(idx)
cursor.execute("INSERT INTO im_entry.test ("+entrym+") VALUES ('"+p+"');")
Run Code Online (Sandbox Code Playgroud)
我正在使用这样的查询,其中entry1,entry2等等是我的数据库表.该程序不显示任何错误,但该p值不会插入数据库中.这有什么不对?请帮我.
int*_*tgr 13
默认情况下,psycopg2会自动为您启动事务,这意味着您必须告诉它提交.注意,这commit是一种方法,而connection不是cursor.
conn = psycopg2.connection('...')
cur = conn.cursor()
cur.execute("...")
conn.commit()
Run Code Online (Sandbox Code Playgroud)
目的是您可以在一个事务中将多个语句组合在一起,因此其他查询不会看到半成品更改,但出于性能原因.
另请注意,您应始终使用占位符,而不是将字符串连接在一起.
例如:
cur.execute("INSERT INTO im_entry.test (colname) VALUES (%s)", [p])
Run Code Online (Sandbox Code Playgroud)
否则,您可能会发生SQL注入攻击.