![](/img/trans.png)
[英]How can I use the column_name of a dataframe as a value on the rows?
[英]how can i SELECT FROM sqlite3_table WHERE column_name IN variable tuple?
我有這個元組:
topics = ('sport', 'math', 'science', 'literature')
每個用戶的這個topics
元組都會改變(他的長度也會改變)
我如何才能從 sqlite3 表中 select 僅在其topic
列的值等於主題元組值之一的行?
我嘗試使用此命令,但沒有成功:
conn = sqlite3.connect('questions_stack.db')
c = conn.cursor()
c.execute("""SELECT * FROM questions WHERE topic IN ?""", subject_tuple)
如果topics
元組的長度每次都變化並且不是恆定的,我可以像這樣從表格中獲取 select 嗎?
如果您能幫助我,我將不勝感激:) 提前致謝!
您需要計算列表中有多少個值,並生成相應的占位符。
就像是:
conn = sqlite3.connect('questions_stack.db')
c = conn.cursor()
binds = ",".join("?" * len(subject_tuple))
sql = '''select * from questions where topic in ({})'''.format(binds)
cur.execute(sql, subject_tuple)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.