kivy with sqlite3

59 views
Skip to first unread message

Ardent Sharma

unread,
May 31, 2020, 11:32:04 PM5/31/20
to Kivy users support
I am new to python and kivy and programming in general. I have the following data base code that I used earlier for tkinter dbms app but now i want to use it with Kivy but I cant seem to find a way to connect it.. I want to keep this file as seperate database file and call it on kivy code as a class; but, I cant seem to find a way to connect the button with the following functionality such as insert,view and so on....
Any help would be much appreciated.

import sqlite3

class Database:

    def __init__(self):
        self.conn=sqlite3.connect('books.db')
        self.cur=self.conn.cursor()
        self.cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY,title text, author text,year integer, isbn integer)")
        self.conn.commit()


    def insert(self,title,author,year,isbn):
        self.cur.execute("INSERT INTO book VALUES(NULL,?,?,?,?)",(title,author,year,isbn))
        self.conn.commit()


    def view(self):

        self.cur.execute("SELECT * FROM book")
        rows=self.cur.fetchall()
        return rows

    def search(self,title='',author='',year='',isbn=''):

        self.cur.execute("SELECT * FROM book WHERE title=? OR author=? OR year=? OR isbn=?",(title,author,year,isbn))
        rows=self.cur.fetchall()
        return rows

    def delete(self,id):

        self.cur.execute("DELETE FROM book WHERE id=? REINDEX book",(id,))
        self.conn.commit()


    def update(self,id,title,author,year,isbn):

        self.cur.execute("UPDATE book SET title=?, author=?, year=?, isbn=? WHERE id=?",(title,author,year,isbn,id))
        self.conn.commit()

    def __del__(self):
        self.conn.close()


Robert Flatt

unread,
Jun 1, 2020, 12:33:45 AM6/1/20
to Kivy users support
In Python, and assuming you have assigned a value to  id :

Button(text="Delete ID",on_press=Database().delete(id))

If you are band new, start with Hello World and replace the Label with a Button
Clearly the app will need several buttons, text entry, and text display in the UI.
Start simple (really simple) and add functionality step by step.

You can if you choose do the same thing in the Kivy language as shown in the Button link.
Message has been deleted

Robert Flatt

unread,
Jun 1, 2020, 1:19:10 AM6/1/20
to Kivy users support
Create a SMALL (one page?) self contained runnable example, and show the EXACT error message.

On Sunday, May 31, 2020 at 7:09:46 PM UTC-10, Ardent Sharma wrote:
I would say I am pretty new but I already have the layout of the app i want...but whenever I try to add the functionality from database into the button widgets defined below I run into some error or it simply does not work and most of the stuff I found online teach to form databse on the main.py file instead.This is the .kv file I have built:

<Manager>:
    Grid:
        name:'main'
    View:
        name:'view'

<Grid>:
    title:title
    author:author
    year_Published:year
    shelf_No:shelf

    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'b.jpg'

    FloatLayout:
        size:root.width,root.height

        Button:
            id:bt1
            text:"Add_Entry"
            font_size: (root.width**2 + root.height**2) / 15**4
            on_press:print('New Book Added!')
            on_release:root.show_popup()
            background_color:(1,0,0,0.2) if bt1.state=='normal' else (0,1,0,1)
            pos_hint:{'x':0,'top':1}
            size_hint: 0.2, 0.4
            color:0,0,1,1

        Button:
            id:bt2
            text:'View_All'
            on_press:print('Showing All Books:')
            on_release:
                app.root.current='view'
                root.manager.transition.direction='right'
            background_color:(1,0,0,0.2) if bt2.state=='normal' else (0,1,0,1)
            pos_hint:{'x':0.2,'top':1}
            size_hint: 0.2, 0.4
            font_size: (root.width**2 + root.height**2) / 15**4
            color:0,0,1,1

        Button:
            id:bt3
            text:'Update_Entry'
            on_press:print('Entry has been Updated!')
            background_color:(1,0,0,0.2) if bt3.state=='normal' else (0,1,0,1)
            pos_hint:{'x':0.4,'top':1}
            font_size: (root.width**2 + root.height**2) / 16**4
            size_hint: 0.2, 0.4
            color:0,0,1,1

        Button:
            id:bt4
            text:'Search'
            on_press:print('Searching....:')
            on_release:
                app.root.current='view'
                root.manager.transition.direction='right'
            background_color:(1,0,0,0.2) if bt4.state=='normal' else (0,1,0,1)
            pos_hint:{'x':0.6,'top':1}
            size_hint: 0.2, 0.4
            font_size: (root.width**2 + root.height**2) / 15**4
            color:0,0,1,1

        Label:
            text:'Title: '
            pos_hint:{'x':0,'top':0.6}
            font_size: (root.width**2 + root.height**2) / 14**4
            size_hint:0.2,0.2

        TextInput:
            id:title
            pos_hint:{'x':0.2,'top':0.6}
            size_hint:0.2,0.3
            font_size: (root.width**2 + root.height**2) / 16**4
            background_color:1,1,1,0.5



        Label:
            text:'Author: '
            pos_hint:{'x':0.4,'top':0.6}
            font_size: (root.width**2 + root.height**2) / 14**4
            size_hint:0.2,0.2


        TextInput:
            id:author
            pos_hint:{'x':0.6,'top':0.6}
            size_hint:0.2,0.3
            font_size: (root.width**2 + root.height**2) / 16**4
            background_color:1,1,1,0.5


        Label:
            text:'Year: '
            multiline:False
            pos_hint:{'x':0,'top':0.3}
            font_size: (root.width**2 + root.height**2) / 14**4
            size_hint:0.2,0.2


        TextInput:
            id:year
            multiline:False
            pos_hint:{'x':0.2,'top':0.3}
            size_hint:0.2,0.3
            font_size: (root.width**2 + root.height**2) / 16**4
            background_color:1,1,1,0.5


        Label:
            text:'Shelf_No: '
            pos_hint:{'x':0.4,'top':0.3}
            font_size: (root.width**2 + root.height**2) / 14.5**4
            size_hint:0.2,0.2


        TextInput:
            id:shelf
            multiline:False
            pos_hint:{'x':0.6,'top':0.3}
            size_hint:0.2,0.3
            font_size: (root.width**2 + root.height**2) / 16**4
            background_color:1,1,1,0.5


        Button:
            id:bt5
            text:'Exit'
            text_orientation:'vertical'
            pos_hint:{'x':0.8,'top':1}
            on_press:app.stop()
            background_color:(0,0,1,0.2) if bt5.state=='normal' else (1,0,0,1)
            font_size:(root.width**2+root.height**2)/14**4
            size_hint:0.2,1
            color:1,0,0,1

<View>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'lib.jpg'

    FloatLayout:
        size:root.width,root.height
        Button:
            text:'Go Back'
            on_release:
                app.root.current='main'
                root.manager.transition.direction='left'
            font_size:(root.width**2+root.height**2)/14**4
            pos_hint:{'x':0,'y':0}
            size_hint:1,0.2
            background_color:1,1,1,0.5

Thank you for your time and response!
 

Ardent Sharma

unread,
Jun 1, 2020, 1:23:46 AM6/1/20
to Kivy users support
I could do that but then I would have to provide several instances of error message for several methods I have tried. Let me ask it this way how do I grab the textinput from the .kv file and insert it into the database using the insert function i have defined. Thanks again!

Robert Flatt

unread,
Jun 1, 2020, 12:59:10 PM6/1/20
to Kivy users support

Walmir Paiva

unread,
Jun 2, 2020, 9:06:30 AM6/2/20
to Kivy users support
    TextInput:
        on_text_validate: root.our_function( self.text)    https://kivy.org/doc/stable/api-kivy.uix.textinput.html
        on_text:               root.our_function( self.text)

Elliot Garbus

unread,
Jun 2, 2020, 10:17:01 AM6/2/20
to Kivy users support
I would suggest the following:
Instance the database as an attribute in the App class.  In the example below, I called it db.

class MyTestApp(App):
def __init__(self, **kwargs):
self.db = Database()
super().__init__(**kwargs)

In you kv code you can access simply as:

Button:
    text: 'Delete'
    on_release: app.db.delete()    # app is a reserve word in kv, and provides a pointer to your app, here the instance of MyTestApp.

In you python code you can access the database methods as follows:

class SomeClass(Widget):
    def use_db_method(self):
        app = App.get_running_app()   # This returns a pointer to the app instance.
        app.db.delete()                          # you can access any of the database methods here
Reply all
Reply to author
Forward
0 new messages