SQLite语句中无法识别的令牌 [英] Unrecognized token in SQLite statement

查看:93
本文介绍了SQLite语句中无法识别的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据从一个表插入到另一个表,但是由于某种原因,我得到了无法识别的令牌".这是代码:

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, longitude, port FROM MessageType1 WHERE latitude>={minlat} AND latitude<={maxlat} AND longitude>= {minlong} AND longitude<= {maxlong}".format(minlat = bottomlat, maxlat = toplat, minlong = bottomlong, maxlong = toplong, table=tablename))

这将转换为以下值:

INSERT INTO db.Vardo SELECT distinct latitude, longitude, port FROM MessageType1 WHERE latitude>=69.41 AND latitude<=70.948 AND longitude>= 27.72 AND longitude<= 28.416

错误代码如下:

sqlite3.OperationalError: unrecognized token: "70.948 AND"

是三个小数点的问题吗?

Is the problem that there is three decimal points?

这是表的创建语句:

cur.execute("CREATE TABLE {site} (latitude, longitude, port)".format(site = site))

推荐答案

不要通过字符串格式进行SQL查询,请使用驱动程序的功能来准备SQL查询并将参数传递到查询中-这样,您可以避免SQL注入,并且可以透明处理不同类型的传递参数:

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))

这篇关于SQLite语句中无法识别的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆