Answer the question
In order to leave comments, you need to log in
How to pass value to sqlite3 command?
I have a users table, for example, I need to find out how many 1 are in column n6 (column with numbers 1,2,3):
cur.execute("SELECT COUNT(*) FROM users WHERE n6 == 1")
xxx = cur.fetchone()
print(xxx)#получаю вывод: (5,)
mmml = 'n' + str(6)
cur.execute("SELECT COUNT(*) FROM users WHERE ? == 1", (mmml, ))
xxx = cur.fetchone()
print(xxx)#получаю вывод: (0,)
Answer the question
In order to leave comments, you need to log in
Value substitution in this form does NOT work for identifiers, which are table and field names. In this case, you can substitute through the usual string formatting:
cur.execute("SELECT COUNT(*) FROM users WHERE {} == 1".format(mmml))
или
cur.execute(f"SELECT COUNT(*) FROM users WHERE {mmml} == 1")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question