用Kivy创建一个sqlite3数据库

时间:2018-12-31 21:08:43

标签: python python-3.x sqlite kivy

我正在尝试使用Kivy在Python 3中使用sqlite3创建数据库。我搜索了整个youtube,堆栈溢出,甚至从O'Reilly买了一本Kivy书来弄清楚这一点。我还尝试使用tkinter,MySQL和JSON来尝试使该功能正常工作……没有骰子。任何建议都很好。预先谢谢你

应该做什么 一个简单的输入框,用于更新和检索学生数据

会发生什么 当我输入数据并尝试更新数据库时,我得到:

AttributeError: 'CreateProfile' object has no attribute 'update_database'

代码

import sqlite3
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.checkbox import CheckBox
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.dropdown import DropDown
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout

conn = sqlite3.connect('student.db')
c = conn.cursor()

class Database():

    def __init__(self):
        super(Database, self).__init__()

    def create_table(self):
        c.execute(
            "CREATE TABLE IF NOT EXISTS studentdata (firstname TEXT, middleinitial TEXT, lastname TEXT, studentid REAL)")
        conn.commit()

    def read_from_db(self):
        c.execute('SELECT * FROM studentdata')
        data = c.fetchall()

    def update_database(self):
        with conn:
            c.execute("INSERT INTO studentdata(firstname, middleinitial, lastname, studentid)\
                      VALUES(?, ?, ?, ?)",
                      (str(self.firstname.get()),
                      str(self.lastname.get()),
                      str(self.middleinitial.get()),
                      str(self.studentid.get()),
                       )
                    )

        c.execute('UPDATE studentdata')
        conn.commit()


class MainScreen(Screen):
    pass

class CreateProfile(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass



presentation = Builder.load_file("formfiller.kv")


class FormFiller(App):
    title = "FormFiller"
    def build(self):
        return presentation


if __name__ == '__main__':
    Database()
    FormFiller().run()
    c.close()
    conn.close()

KV文件

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition:FadeTransition()
    MainScreen:
    CreateProfile:

<Button@Button>:
    font_size: 20

<MainTitle@Label>:
    font_size: 50
    size_hint_y: 1.9
    size_hint_x: 1
    bold: True

<SubTitle@Label>:
    font_size: 25
    size_hint_y: 1.70
    size_hint_x: 1
    bold: True

<InputField@Label>:
    font_size: 18
    bold: True

<TextInput@TextInput>:
    font_size: 16
    bold: True
    height: self.line_height

<update_database>:


<MainScreen>:
    name: "main"
    MainTitle:
        id: main_title
        text: "Packet Filler"

    SubTitle:
        text: "Making Your Life Easier With the Push of a Button!"

    Button:
        on_release: app.root.current = "create_profile"
        text: "Create New Profile"
        pos_hint: {'x':.15, 'y':.45}
        size_hint: (.25, .1)

    Button:
        text: "Load Profile"
        pos_hint: {'x':.65, 'y':.45}
        size_hint: (.25, .1)

<CreateProfile>:
    name: "create_profile"
    MainTitle:
        id: main_title
        text: "Create New Profile"

    SubTitle:
        text: "Input Your Data"

    Button:
        text: "Create"
        pos_hint: {'x':.15, 'y':.02}
        size_hint: (.25, .1)
        on_release: root.update_database()

    InputField:
        text: "First Name:"
        size_hint_y: 1.35
        size_hint_x: .15

    TextInput:
        size_hint_y: .05
        size_hint_x: .15
        multiline: False
        pos_hint: {'x':.15, 'y':.65}

    InputField:
        text: "Middle Initial:"
        size_hint_y: 1.2
        size_hint_x: .15

    TextInput:
        size_hint_y: .05
        size_hint_x: .15
        multiline: False
        pos_hint: {'x':.15, 'y':.57}

    InputField:
        text: "Last Name:"
        size_hint_y: 1.05
        size_hint_x: .15

    TextInput:
        size_hint_y: .05
        size_hint_x: .15
        multiline: False
        pos_hint: {'x':.15, 'y':.50}

跟踪

 Traceback (most recent call last):
   File "/home/jarren/PycharmProjects/BCO_Form_Filler/formfiller.py", line 70, in <module>
     FormFiller().run()
   File "/usr/lib/python3/dist-packages/kivy/app.py", line 826, in run
     runTouchApp()
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 727, in mainloop
     self._mainloop()
   File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 460, in _mainloop
     EventLoop.idle()
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 340, in idle
     self.dispatch_input()
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 325, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/usr/lib/python3/dist-packages/kivy/base.py", line 291, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
   File "/usr/lib/python3/dist-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
   File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
   File "kivy/_event.pyx", line 1098, in kivy._event.EventObservers._dispatch
   File "/usr/lib/python3/dist-packages/kivy/lang/builder.py", line 64, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "/home/jarren/PycharmProjects/BCO_Form_Filler/formfiller.kv", line 68, in <module>
     on_release: root.update_database()
   File "kivy/weakproxy.pyx", line 30, in kivy.weakproxy.WeakProxy.__getattr__
 AttributeError: 'CreateProfile' object has no attribute 'update_database'

2 个答案:

答案 0 :(得分:1)

错误是CreateProfile内部没有名为Update_database的函数... 您有一个类名Update_database,但不在createprofile中,而在数据库中 它有两个不同的类...

答案 1 :(得分:0)

我发现this answer帮助我完全理解了如何将kivy与sqlite3关联。不仅如此,它还具有我喜欢的布局;遗憾的是,更早发现并不容易。谢谢大家的帮助