0

new error: near "%": syntax error I have multiple list with values, and i want to safe them line by line in an postgresql database

i mangaged to do it with sql, but postgresql doesn´t work the same, whats the difference?

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 version i hope for no error and all lines safed in psql thanks for any help info: the lists have all the same length, idk what wrong

  • You should use `%s` instead of `?` and pass your args as a tuple to the second argument of `cur.execute(sql, (myargs,))`. – Error - Syntactical Remorse Jul 18 '19 at 17:17
  • 1
    Possible duplicate of [Using python and postgres, variables within execute function?](https://stackoverflow.com/questions/24789231/using-python-and-postgres-variables-within-execute-function) – Error - Syntactical Remorse Jul 18 '19 at 17:19
  • Any Other ideas to this Part: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 – Marius Buchholz Jul 18 '19 at 18:00

0 Answers0