简体   繁体   English

在sqlite3语句中使用Python变量

[英]Using Python variables within sqlite3 statement

I would like to use python variable within a sql request. 我想在sql请求中使用python变量。

ss_length = 15

I tried this : 我尝试了这个:

cursor = db.execute("SELECT SUBSTR(timestamp, 1, %s), count(SUBSTR(timestamp, 1, %s)) FROM tweets GROUP BY SUBSTR(timestamp, 1, %s) ORDER BY count(SUBSTR(timestamp, 1, %s))" % ss_length, ss_length, ss_length, ss_length)

and this 和这个

cursor = db.execute("SELECT SUBSTR(timestamp, 1, ?), count(SUBSTR(timestamp, 1, ?)) FROM tweets GROUP BY SUBSTR(timestamp, 1, ?) ORDER BY count(SUBSTR(timestamp, 1, ?))", ss_length, ss_length, ss_length, ss_length)

No one of the two worked for me. 两者中没有一个为我工作。

What's wrong with them ? 他们怎么了 ?

I used this and it worked, thank you all for your responses: 我用了它,它起作用了,谢谢大家的答复:

cursor = db.execute("""SELECT SUBSTR(timestamp, 1, ?), count(SUBSTR(timestamp, 1, ?)) 
                       FROM tweets 
                       GROUP BY SUBSTR(timestamp, 1, ?) 
                       ORDER BY count(SUBSTR(timestamp, 1, ?))
                    """, (ss_length, ss_length, ss_length, ss_length))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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