Re: [kivy-users] SQLite database editing with kivy and RecycleView

493 views
Skip to first unread message

Andreas Ecker

unread,
Mar 26, 2020, 11:52:39 AM3/26/20
to kivy-...@googlegroups.com
Having no experience with the RecycleView, so I had to play a while with your code example to get this working. What you're code is mainly missing:
- save data in your `update_changes` method into the database.
- detecting the primary key of the associated db row and the db field name that got changed by your popup.

Screenshot from 2020-03-26 15-42-24.png
In the above screenshot you can see that all columns can be updated with the amended kv/py files of your example  (see attached).

HTH and good luck - there is still a lot to do for you, like e.g. ensure the ID (primary key of your db table) should never be changeable...

Am Mi., 25. März 2020 um 23:15 Uhr schrieb Thelmo John Cunanan <tj.cu...@gmail.com>:

I'm trying to display an sqlite3 database in python using kivy. Tested out ikolim's solution here and it works as intended in displaying the database contents into the RecycleView's button labels:

enter image description here

And when a button is pressed, a popup and edit function appears as intended:

enter image description here

And it does edit the selected button's text as shown in the next screenshot.. :

enter image description here

..but not the sqlite database values. Would it be possible to reflect the changes the user makes inside the Kivy app to the values inside the sqlite database?

Here are the codes:

test.py

import sqlite3

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup

connection = sqlite3.connect("demo.db", isolation_level=None)
cursor = connection.cursor()

class TextInputPopup(Popup):
    obj = ObjectProperty(None)
    obj_text = StringProperty("")

    def __init__(self, obj, **kwargs):
        super(TextInputPopup, self).__init__(**kwargs)
        self.obj = obj
        self.obj_text = obj.text


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected

    def on_press(self):
        popup = TextInputPopup(self)
        popup.open()

    def update_changes(self, txt):
        self.text = txt


class RV(BoxLayout):
    data_items = ListProperty([])

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):

        cursor.execute("CREATE TABLE IF NOT EXISTS Callbacks(cName TEXT, cID INT, cbTime INT, cbRems TEXT)")
        cursor.execute("INSERT INTO Callbacks VALUES ('Client1','1','1500','Test1')")
        cursor.execute("INSERT INTO Callbacks VALUES ('Client2','2','1600','Test2')")
        cursor.execute("INSERT INTO Callbacks VALUES ('Client3','3','1700','Test3')")
        connection.commit()
        cursor.execute("SELECT * FROM Callbacks ORDER BY ROWID DESC")

        rows = cursor.fetchall()

        # create data_items
        for row in rows:
            for col in row:
                self.data_items.append(col)

        for row in rows:
            print(self.data_items)



class TestApp(App):
    title = "Kivy RecycleView & SQLite3 Demo"

    def build(self):
        return RV()


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.10.0

<TextInputPopup>:
    title: "Popup"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        TextInput:
            id: txtinput
            text: root.obj_text
        Button:
            size_hint: 1, 0.2
            text: "Save Changes"
            on_release:
                root.obj.update_changes(txtinput.text)
                root.dismiss()
        Button:
            size_hint: 1, 0.2
            text: "Cancel Changes"
            on_release: root.dismiss()


<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    BoxLayout:
        orientation: "vertical"

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 4

            Label:
                text: "Name"
            Label:
                text: "ID"
            Label:
                text: "CB Time"
            Label:
                text: "Remarks"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.data_items]
                SelectableRecycleGridLayout:
                    cols: 4
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d68a1a04-3c7a-4c36-8907-4132f342c574%40googlegroups.com.
db_grid.py
db_grid.kv

Thelmo John Cunanan

unread,
Mar 31, 2020, 6:41:11 AM3/31/20
to kivy-...@googlegroups.com
I have tried your code and it works fine! Thank you for this solution!
However, would it be possible to use ROWID instead of the cID as the parameter for WHERE? So that cID would also be editable.
Thank you again for your help! 



    def build(self<span style="font-style:inherit;font-variant:inherit;font-

Andreas Ecker

unread,
Mar 31, 2020, 12:19:22 PM3/31/20
to kivy-...@googlegroups.com
My "working solution" is an ugly hack and not for production code - I just want to demonstrate to you that a data UPDATE can be done.

A new attached code example is now using `ROWID` (instead of the `cID` column) for to identify the DB record to update (so now you can have now more than one db row with the same value in the `cID` column)...

FYI: Actually in my last code example I redeclared the `cID` column as the primary key. In general I'd recommend to not use the `ROWID` column - some hints when and why not to use `ROWID` in Sqlite DBs you find here: https://www.sqlite.org/rowidtable.html

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
db_grid.py
db_grid.kv

Electronic Part

unread,
Apr 1, 2020, 8:44:05 AM4/1/20
to kivy-...@googlegroups.com
Do you guys know how to Automatically update RecycleView when SQLite tables change or its data?

class TestApp(App):<span style="font-sty
Reply all
Reply to author
Forward
0 new messages