Skip to content Skip to sidebar Skip to footer

Unrecognized Token In Sqlite Statement

I am inserting data from one table to another, however for some reason I get 'unrecognized token'. This is the code: cur.execute('INSERT INTO db.{table} SELECT distinct latitude, l

Solution 1:

Don't make your SQL queries via string formatting, use the driver's ability to prepare SQL queries and pass parameters into the query - this way you would avoid SQL injections and it would make handling of passing parameters of different types transparent:

query = """
    INSERT INTO 
        db.{table} 
    SELECT DISTINCT
        latitude, longitude, port 
    FROM 
        MessageType1 
    WHERE 
        latitude >= ? AND 
        latitude <= ? AND 
        longitude >= ? AND 
        longitude <= ?
""".format(table=tablename)
cur.execute(query, (bottomlat, toplat, bottomlong, toplong))

Solution 2:

Try using ? for your parameters:

cur.execute("INSERT INTO db.? SELECT distinct latitude, longitude, port FROM MessageType1 WHERE latitude>=? AND latitude<=? AND longitude>= ? AND longitude<= ?",(bottomlat, toplat, bottomlong, toplong, tablename))

Post a Comment for "Unrecognized Token In Sqlite Statement"