How to pass a variable from one function to an Sqlite3 query in another function in Python/Kivy -
i building app using python 2.7, kivy , sqlite3. having trouble passing variable value text_input widget(date picker kivy garden) value in function choose_date_water
, passing function view_water_figures
can used in querying sqlite table. have been trying days figure out without luck , hoping little help.
- how pass variable first function second?
- am querying sqlite table correctly using variable
dp1
?
the relevant code:
class logsheet(tabbedpanel): def choose_date_water(self): box = floatlayout() box.add_widget(label(text = "select date view", font_size = (30), pos_hint = {'center_x':0.5, 'center_y': 0.9 })) #box.add_widget(textinput(hint_text = "dd/mm/yyyy", font_size = (33), size_hint = (none, none), size = (300, 50), pos_hint = {'center_x':0.5, 'center_y': 0.6 })) dp1 = box.add_widget(datepicker(phint_x = (0.35), phint_y = (0.55), size_hint = (none, none), size = (190, 50), font_size = (33), pos_hint = {'center_x':0.5, 'center_y': 0.6 })) btn1 = button(text = "ok", size_hint = (none, none), size = (200, 50), pos_hint = {'center_x':0.5, 'center_y': 0.25 }) box.add_widget(btn1) popup1 = popup(title = "choose date", title_size = (40), title_align = 'center', content = box, size_hint = (none, none), size = (600, 300)) btn1.bind(on_press = self.view_water_figures, on_release = popup1.dismiss) popup1.open() return dp1 def view_water_figures(self): conn = sqlite3.connect('logsheet.db') c = conn.cursor() conn.execute("select today_total_dw_vol, today_total_fw_vol, total_evap_out waterfigures date = ?", (dp1,)) conn.commit() wf = c.fetchall() item in wf: i1 = str(item[1]) i2 = str(item[2]) i3 = str(item[3]) box = floatlayout() box.add_widget(label(text = "total dist water used: " + i1, pos_hint={'center_x':0.15, 'center_y': 0.9 })) box.add_widget(label(text = "total fresh water used: " + i2, pos_hint={'center_x':0.15, 'center_y': 0.8 })) box.add_widget(label(text = "total #1 evap produced: " + i3, pos_hint={'center_x':0.15, 'center_y': 0.7 })) btn1 = button(text = "ok", size_hint = (none, none), size = (200, 50), pos_hint={'center_x': 0.25, 'center_y': .1}) btn2 = button(text = "export data excel", size_hint = (none, none), size = (200, 50), pos_hint={'center_x': 0.75, 'center_y': .1}) box.add_widget(btn1) box.add_widget(btn2) popup2 = popup(title='water figures', title_size= (30), title_align = 'center', content = box, size_hint=(none, none), size=(600, 500)) btn1.bind(on_release = popup2.dismiss) popup2.open() c.close() conn.close()
make date picker member of logsheet
class using self
. can access other methods (functions) of same class using self.dp1
:
def choose_date_water(self): box = floatlayout() box.add_widget(label(text = "select date view", font_size = (30), pos_hint = {'center_x':0.5, 'center_y': 0.9 })) self.dp1 = box.add_widget(datepicker(phint_x = (0.35), phint_y = (0.55), size_hint = (none, none), size = (190, 50), font_size = (33), pos_hint = {'center_x':0.5, 'center_y': 0.6 })) # etc...
now self.dp1
member of class , accessible in other methods of class. can access in view_water_figures()
this:
def view_water_figures(self): conn = sqlite3.connect('logsheet.db') c = conn.cursor() c.execute("select today_total_dw_vol, today_total_fw_vol, total_evap_out waterfigures date = ?", (self.dp1.text,)) wf = c.fetchall() # etc...
firstly use cursor run query, not connection. secondly use self.dp1
access date picker. date picker provides text
attribute contains date value selected user (not sure widget you're using).
your use of parameterised query correct, however, not necessary call conn.commit()
when performing read operations on database.
Comments
Post a Comment