简体   繁体   中英

how to insert variable in sqlite3 request python

Dears,

how can I check if pos_cli from database is equal to variable pos_id? for now with code below I get the following error

cur.execute("CREATE TABLE IF NOT EXISTS Magnit_Coor (pos_cli INTEGER PRIMARY KEY, lat INTEGER, long INTEGER);")
cur.execute('SELECT * FROM Magnit_pos')
data = cur.fetchall()
while True:
    for coo in data:
        full_add = coo[6:11]
        pos_id = coo[0]
        print (pos_id)
        yand_add = ", ".join(full_add)
        g = cur.execute('SELECT EXISTS (SELECT * FROM Magnit_Coor WHERE pos_cli = (?))',pos_id)
        g = cur.fetchone()[0]

error below

10001
Traceback (most recent call last):
  File "geoco.py", line 17, in <module>
    g = cur.execute('SELECT EXISTS (SELECT * FROM Magnit_pos WHERE pos_cli = (?))',pos_id)
ValueError: parameters are of unsupported type

The initial code to create Magnit_pos table and pos_cli especially below

cur.execute("DROP TABLE IF EXISTS Magnit_Pos;")
cur.execute(
    "CREATE TABLE Magnit_Pos (pos_cli INTEGER PRIMARY KEY, magnit_name TEXT, codesfa TEXT, codewsot TEXT, pos_sap TEXT, source_dc TEXT, zip TEXT, region TEXT, area TEXT, city TEXT, street TEXT, house TEXT, build TEXT);")

with open('magnit.csv') as csvfile:
    magnit = csv.reader(csvfile, delimiter=';')
    print(magnit)
    for row in magnit:
        print(row[0])
        # to_db = [unicode(row[0], "utf8"), unicode(row[1], "utf8")]
        cur.execute("INSERT INTO Magnit_Pos (pos_cli, magnit_name, codesfa, codewsot, pos_sap, source_dc, zip, region, area, city, street, house, build) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", row)

From python's sqlite3 documentation (emphasis mine):

Put ? as a placeholder wherever you want to use a value, and then provide a tuple of values as the second argument to the cursor's execute() method.

So you should be using:

    g = cur.execute('SELECT EXISTS (SELECT * FROM Magnit_Coor WHERE pos_cli = (?))',(pos_id,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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