2

I'm trying to create a database using sqlite3 in Python 3 using Kivy. I've searched all over youtube, stack overflow, and even bought a Kivy book from O'Reilly to figure this out. I've also played around with tkinter, MySQL, and JSON to try and get the function to work...no dice. Any suggestions would be great. Thank you in advance

What it's supposed to do A simple input box to update and retrieve student data

What Happens When I input the data and try to update the database I get:

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

Code

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 File

#: 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

 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'
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jarren Poulsen
  • 141
  • 1
  • 13

2 Answers2

1

the error is that there is not function inside CreateProfile that is name Update_database... you have a class name Update_database but is not inside of the createprofile is inside database its two different classes....

Cesar Rodriguez
  • 192
  • 1
  • 15
1

I found this answer that helped me fully understand how to link kivy with sqlite3. Not only that but it has a layout I like; shame it wasn't easier to find earlier. Thank you all for your help

Jarren Poulsen
  • 141
  • 1
  • 13