首页 > 解决方案 > sqlite3.OperationalError:没有这样的列:current_month_value

问题描述

在这段代码中,我试图过滤我的数据库以获取用户点击的某些月份,它由一个名为 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()

标签: python-3.xsqlitekivykivy-language

解决方案


curso.execute("SELECT * FROM page3 WHERE Month=?", (current_month_value,))

在您的版本中,您将current_month_value字符串常量放入其中,因此 sqlite 认为它是列名,就像Month. 您应该将其定义为参数,使用问号(“?”)并将此参数绑定到它的值,通过current_month_value变量传递它


推荐阅读