简体   繁体   English

Psycopg2 INSERT变量,字符串格式错误

[英]Psycopg2 INSERT variables, string format error

I am trying to update a row of a Postgresql table. 我正在尝试更新Postgresql表的行。 I am using Psycopg2 and Python3. 我正在使用Psycopg2和Python3。

I receive the error: "TypeError: not all arguments converted during string formatting" 我收到错误:“ TypeError:在字符串格式化期间并非所有参数都已转换”

code: self.cursor.executemany("INSERT INTO blacklisted_ips (ip, source_id, date_created, date_modified) VALUES (%s, %s, %s, %s);", ["address_list"]) 代码:self.cursor.executemany(“ INSERT INTO blacklisted_ips(ip,source_id,date_created,date_modified)VALUES(%s,%s,%s,%s);”,[“地址列表”])

ip, source_id, date_created, date_modified are the column names. ip,source_id,date_created和date_modified是列名。

passing a list of tuples address_list sample data: ('223.223.202.183', 29, '2018-06-28 12:32:02', '2018-06-28 12:32:02') 传递元组列表address_list示例数据:('223.223.202.183',29,'2018-06-28 12:32:02','2018-06-28 12:32:02')

I can't figure out why I get this error. 我不知道为什么会收到此错误。 I'm guessing it is centered around the place holders after "VALUES". 我猜想它以“ VALUES”之后的占位符为中心。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Assuming address_list is a list of values to be inserted, you can pass it directly to executemany : 假设address_list是要插入的值的列表,则可以将其直接传递给executemany

self.cursor.executemany("INSERT INTO blacklisted_ips (ip, source_id, date_created, date_modified) VALUES (%s, %s, %s, %s);", address_list)

(Note the lack of quotes and brackets around address_list .) (请注意, address_list周围没有引号和括号。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM