在Python中使用变量作为LIKE语句进行SQL查询

时间:2019-05-01 08:46:05

标签: python sql python-2.7 sqlite

尝试从数据库的列中获取某些信息以创建.csv文件。创建的代码可以成功查询单行,但是必须为数据库中的每一行获取相同的信息。每个类别都有多行,因此我必须使用LIKE语句。

#code in python for querying and printing multiple rows for a category
data = c.execute("SELECT x,y, CAST( (direction*180/3.142)as INT),CAST(quality*100 as INT) from UTS_6_fingerprints where finger like 'lt_94' ")
for row in data:
    print row

1 个答案:

答案 0 :(得分:0)

只需使用参数。例如:

data = c.execute("""
SELECT x, y
       CAST( (direction*180/3.142)as INT) as col3,
       CAST(quality*100 as INT) as col4
FROM UTS_6_fingerprints 
WHERE finger LIKE :pattern
""", {'pattern', 'lt_94'})