在这段代码中,我试图过滤数据库以使用户单击某些月份,并且该数据库由一个名为current_month_value的变量保存。每当我使用curso.execute(“ SELECT * FROM page3 WHERE Month = current_month_value”)查询数据库时。它给出了一个错误消息,即不存在此类列,请问我该如何修改此查询以仅获取被单击的月份。
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.properties import BooleanProperty, ListProperty, ObjectProperty, StringProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.button import Button
import sqlite3 as sql
box=[]
butto=""
butt=""
# Paths
path = 'C:\\Users\\USA GADGETS\\PycharmProjects\\App\\first_page.db'
path1 = 'C:\\Users\\USA GADGETS\\PycharmProjects\\App\\second_page.db'
path2 = 'C:\\Users\\USA GADGETS\\PycharmProjects\\App\\third_page.db'
path3 = 'C:\\Users\\USA GADGETS\\PycharmProjects\\App\\fourth_page.db'
path4 = 'C:\\Users\\USA GADGETS\\PycharmProjects\\App\\fifth_page.db'
# connections
connec=sql.connect(path2) # conect to third page database
fourth_page_connec = sql.connect(path3) # conect to fourth page database
fifth_page_connec = sql.connect(path4) # conect to fifth page database
# Cursor
curso=connec.cursor() # Third page cursor
cursoo=fourth_page_connec.cursor() # Fourth page cursor
cursoor=fifth_page_connec.cursor() # Fifth page cursor
# pass a query
curso.execute("""CREATE TABLE IF NOT EXISTS page3(id integer primary key autoincrement,Month,Description text,Amount REAL)""") # Third Page Query
connec.commit() # Third Page Commit
cursoo.execute(""" CREATE TABLE IF NOT EXISTS page4(Items,Description text,Amount REAL)""")
fourth_page_connec.commit()
cursoor.execute(""" CREATE TABLE IF NOT EXISTS page5(id integer primary key autoincrement,Items,Description text,Amount REAL)""")
fifth_page_connec.commit()
# Close connection
connec.close()
fourth_page_connec.close()
fifth_page_connec.close()
# declaration of properties to modify objects from my.kv
finances= ObjectProperty()
month=ObjectProperty()
jan_action=ObjectProperty()
feb_action=ObjectProperty
mar_action=ObjectProperty()
apr_action=ObjectProperty
may_action=ObjectProperty()
jun_action=ObjectProperty
jul_action=ObjectProperty()
aug_action=ObjectProperty
sep_action=ObjectProperty()
oct_action=ObjectProperty
nov_action=ObjectProperty()
dec_action=ObjectProperty
# declaration of properties of second page
income_action=ObjectProperty()
budget_action=ObjectProperty
event_action=ObjectProperty()
analysis_action=ObjectProperty
report=ObjectProperty()
class FirstScreen(Screen):
def Pressbutton(self,button):
global butt
butt=button
return butt
class SecondScreen(Screen):
def Pressbutton2(self,button):
global butto
butto=button
return butto.text
class ThirdScreen(Screen):
desc_action = ObjectProperty()
amount_action = ObjectProperty()
data_items1=ListProperty(["S/N","MONTH","INCOME","AMOUNT"]) # Headings for columns in recycle viewer
# To identify only the month chosen at the first instance
def getFromIncome(self):
def __init__(self, **kwargs):
super(ThirdScreen, self).__init__(**kwargs)
current_month_value = FirstScreen().Pressbutton(butt)
connec = sql.connect("third_page.db")
# Cursor
curso = connec.cursor()
# pass a query
curso.execute("""INSERT INTO page3(
id,Month,Description,Amount) VALUES (NULL,?,?,?) """,
(current_month_value,self.desc_action.text,self.amount_action.text))
curso.execute("SELECT * FROM page3 WHERE Month=current_month_value")
rows= curso.fetchall()
connec.commit()
for row in rows:
for col in row:
self.data_items1.append(col)
# Close connection
connec.close()
答案 0 :(得分:0)
curso.execute("SELECT * FROM page3 WHERE Month=?", (current_month_value,))
在您的版本中,您将current_month_value
放在字符串常量中,因此sqlite认为它是列名,就像Month
一样。您应该使用问号(“?”)将此参数定义为参数,并将其绑定到其值,并通过current_month_value
变量