如何在postgresql语句中插入值,错误消息:并非所有参数都在字符串格式化期间转换

2024-06-16 12:45:49 发布

您现在位置:Python中文网/ 问答频道 /正文

新错误:“%”附近:语法错误 我有多个带有值的列表,我想在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)

--编辑版本 我希望没有错误和所有的线路安全的psql 谢谢你的帮助 信息:列表的长度都一样,idk怎么了


Tags: thesqltimedatabasespeedlonlatele