python - 如何在 postgresql 语句中插入值,错误消息:字符串格式化期间并非所有参数都转换
问题描述
新错误:“%”附近:语法错误我有多个包含值的列表,我想在 postgresql 数据库中逐行保护它们
我设法用sql来做,但是postgresql不一样,有什么区别?
def safePsql(TrackID,seglist,trkptlist,speed,ele,time,lat,lon,hdop,mode,länge):
sql = "INSERT INTO mobilenew VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s);"
#mobilenew
e = 0
modedata = []
for e in range(länge):
e= e+1
modedata.append(mode)
#vendor_list = [TrackID,seglist[f],trkptlist[f],lat[f],lon[f],ele[f],time[f],hdop[f],speed[f],modedata[f]]
try:
# read database configuration
#params = config()
# connect to the PostgreSQL database
#conn = psycopg2.connect(**params)
# create a new cursor
#cur = conn1.cursor()
conn1 = psycopg2.connect("dbname='sdb_course' user='postgres' host='localhost' password='admin'")
d = conn1.cursor()
# execute the INSERT statement
f = 0
for f in range(länge):
d.execute(sql , (TrackID,seglist[f],trkptlist[f],lat[f],lon[f],ele[f],time[f],hdop[f],speed[f],modedata[f]))
f = f+1
# commit the changes to the database
conn1.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
--edited 版本我希望没有错误,并且 psql 中的所有行都安全感谢任何帮助信息:列表长度相同,idk 出了什么问题
解决方案
推荐阅读
- c++ - C++模板参数指定第二个
- javascript - 构造html列匹配类
- api - 需要在 SharedPreferences 中保存我的登录 api json 响应
- azure - 有没有办法从天蓝色搜索索引中删除旧数据
- firebase - Flutter & Firebase:如何在我刚创建文档时将 documentID 放入文档字段?
- bash - 阅读以逗号分隔的每个单词
- javascript - 在 Material UI Chip 中对齐头像中心
- java - 在 Java 中使用字符数组执行数学运算的最有效方法是什么?
- react-native - 如何在 TensorflowJs 中将张量解码为 base64?
- node.js - 将标头发送到客户端后无法设置标头 - Node/Express/JWT