我有以下函数应该批量插入IPv6 IP列表
def insertToDb(ipList):
print("OK")
try:
conn = psycopg2.connect("dbname='mydb' user='myuser' host='hanno.db.elephantsql.com' password='mypass'")
except:
print("I am unable to connect to the database")
cur = conn.cursor()
try:
cur.executemany("INSERT INTO ip (ip) VALUES(%s)", ipList)
conn.commit()
except Exception as e: print(e)
print("Inserted!")
我收到以下消息
并非在字符串格式化期间转换所有参数
需要正确的格式是什么?
答案 0 :(得分:1)
使用Logger logger = LoggerFactory.getLogger(HttpUtil.class);
HttpLoggingInterceptor logging =
new HttpLoggingInterceptor((msg) -> {
logger.debug(msg);
});
logging.setLevel(Level.BODY);
client.addNetworkInterceptor(logging);
,您可能只需要将executemany
转换为如下所示的元组列表:
ipList
或者您也可以将列表的构建方式更改为:
params = [(ip,) for ip in iplist]
cur.executemany("INSERT INTO ip (ip) VALUES(%s)", params)