Python Forum

Full Version: which column of sql table is equal to a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i want to find the value of 'vazn' (one column of sql table) where 'id' column is equal to f1.
f1 is a variable as following:
f1=int(enter3.get())
enter3 is an entry.
i entered the variable in enter3 that is equal to one of the existing 'id's in the sql table('bills')
this is my code:

Output:
[self.cur.execute("SELECT vazn FROM billse WHERE id1='f1'")                 vaznp = self.cur.fetchall()                 print(vaznp)][/python] i get 'null' when it prints vaznp
why?????
please helpppp Sick
vazno, id1 are variables
billsa is the  table (or view) name

It's telling you nothing matches id1
(Mar-13-2017, 12:48 PM)Larz60+ Wrote: [ -> ]vazno, id1 are variables
billsa is the  table (or view) name

It's telling you nothing matches id1

the table name is "billse"
"vazn" & "id1" are the columns of this table...
"f1" is a variable
Of course you are correct:
bottom line is still correct:
It's telling you nothing matches id1 (f1)

suggest a print statement to see contents of f1
(Mar-13-2017, 01:40 PM)Larz60+ Wrote: [ -> ]Of course you are correct:
bottom line is still correct:
It's telling you nothing matches id1 (f1)

suggest a print statement to see contents of f1

i have seen  contents of f1...it's "789"...one of values from "id1" column is "789" too...but i get "null" for "self.cur.fetchall()" or the same "vaznp"
why???????????
help Sad
But you are trying to get rows with id = "f1", not rows with id = "789" - you have no variable substitution/expansion in your select string. So you are getting the correct response (unless you have row with id "f1" in your table ... ). 

Either format/construct your select string with your variable - you can do something like:
select_string = 'select * from bla where id = "{}";'.format(f1)
or use (and thats better solution, if its available) a variable substitution provided by a database connection - its usually something like:
cur.execute("select * from bla where id = ?", (f1,))
exact syntax might vary according to the database used.
ok.my problem was solved...but i have a new problem..please help again...i want to add **vazne** into another table(it maeans **vazne** will becomes one of the the second table columns )...when i print **vazne** , it returns correct value but as this format : [(360,)]...so when i add it into the second table,

i get the following error
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "/home/pi/start/gui/jus-submeno-image-refreshh.py", line 609, in tozin2
self.whtable()
File "/home/pi/start/gui/jus-submeno-image-refreshh.py", line 645, in whtable
c.execute("INSERT INTO billsf (vaznf, id1, firs1,las1, yekdo, vaznemp) VALUES (?, ?, ?, ?, ?, ?)",(counter, f1, d1, e1, yord, vazne))
sqlite3.InterfaceError: Error binding parameter 5 - probably unsupported type.

**billse** is the first table...**billsf** is the second table... **vazne** is selected based on 2 items in the first second...after calling **vazne** from the first table, it shoud be added to the second table(**billsf**)
(Mar-15-2017, 07:58 AM)gray Wrote: [ -> ]ok.my problem was solved...but i have a new problem..please help again...

You already opened a new thread for that: https://python-forum.io/Thread-combining...ecial-form

Any discussion of the new question, should take place in the new thread.  I'm not closing this thread in case someone still wants to discuss this topic.