I am struggling to replicate an sqlite SELECT query.
sqlite> select "de-515" from balances where id = '1';
de-515
0.1
sqlite>
works with no issue from sqlite, however when I try building this query into a python script nothing I put together seems to work. I have tried the following with the intent of setting a python variable equal to the 0.1 value that is seen above.
elementBalQuery = 'SELECT "%s" FROM balances WHERE id="%s"'
cursor.execute(elementBalQuery, (elementName,userId))
curBal = cursor.fetchall()
print("curBal = ", curBal)
this results in
Traceback (most recent call last):
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 0, and there are 2 supplied.
Also this fails
cursor.execute("SELECT %s FROM balances WHERE id=?", % (elementName), (userId))
cursor.execute("SELECT %s FROM balances WHERE id=?", % (elementName), (userId))
SyntaxError: invalid syntax
I have attempted several other ways to craft this query and cannot figure out what I am missing. I come from a bash background where I would be able to easily set a var equal to the output of a sqlite query.
How would, using a dynamic query, I select a row using a variable name?