【问题标题】:sqlite3.OperationalError: no such column: current_month_valuesqlite3.OperationalError:没有这样的列:current_month_value
【发布时间】:2021-01-27 07:07:57
【问题描述】:

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


    【解决方案1】:
    curso.execute("SELECT * FROM page3 WHERE Month=?", (current_month_value,))
    

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

    【讨论】:

    • 虽然这段代码可能会解决问题,但一个好的答案还应该解释代码的什么以及它如何提供帮助。
    • 在运行您编写的代码时,它会给出 sqlite3.ProgrammingError: Incorrect number of bindings provided。当前语句使用 1,并且提供了 0。
    • 您的变量是否可能在引号内而不是在引号外?您是否在“?”之后关闭了字符串?签到?
    • 我编辑了答案。参数必须作为元组传递,如下所述:stackoverflow.com/questions/16856647/…
    • 它成功了先生,谢谢,但有一个小问题。首先,我点击的月份的值没有显示在月份表下。其次,假设我一直选择 3 月作为我的月份,突然间我选择了 4 月,我希望它能给我提供关于最后一个选择的数据,即 4 月,但我仍然看到它会将 3 月的结果带入其中。先生,无论如何我可以再次修改查询吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-01-12
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多