[PYTHON] I tried using a database (sqlite3) with kivy

I tried using the database (sqlite3) with __kivy __

I tried using sqlite3 for the first time, so make a note of it.

A simple sample is below. Basically, when learning a new thing (framework), I try to create my ***. Py or one file for each function.

mystorage.py





from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.storage.jsonstore import JsonStore

from datetime import datetime
import sqlite3



class MyStorageApp(App):

    conn = sqlite3.connect("./data.db")
    cur = conn.cursor()

    mydata = JsonStore('./mydata.json')

    countnum = 0
    now="now"

    def gettime(self):
        return str(datetime.now())

    def getData(self):
        self.entry = self.mydata.get('data')

        print(self.entry)


# layout
    def build(self):

        layout = BoxLayout(padding=10, orientation='vertical')
        btn1 = Button(text="PUSH")
        btn1.bind(on_press=self.buttonClicked)

        self.lbl1 = Label(text = "count is "+str(self.countnum))
        self.lbl2 = Label(text = self.now)

        layout.add_widget(btn1)
        layout.add_widget(self.lbl1)
        layout.add_widget(self.lbl2)

        return layout

# button click function
# "bind" need "instance" arg (this case is "btn" arg)
#http://stackoverflow.com/questions/23127203/bind-function-to-kivy-button
    def buttonClicked(self,btn):
        self.countnum += 1
        self.lbl1.text = "count is " +str(self.countnum)

        self.now= self.gettime()

        self.lbl2.text = self.now

        try:
            self.cur.execute("""CREATE TABLE times(time text, count int);""")
        except:
            pass

        self.cur.execute('''INSERT INTO times VALUES(:time, :count)''',{'time':self.now,'count':self.countnum})
        self.conn.commit()

        self.cur.execute("select * from times")
        for row in self.cur:
            print row[0],row[1]
            self.mydata.put('data', mytime=row[0],mycount=row[1])

        self.getData()

        print("done")

# run app
if __name__ == "__main__":
    MyStorageApp().run()



Recommended Posts

I tried using a database (sqlite3) with kivy
I tried using Amazon SQS with django-celery
I tried playing a ○ ✕ game using TensorFlow
I tried using Selenium with Headless chrome
I tried drawing a line using turtle
I tried a functional language with Python
I tried using pipenv, so a memo
I tried to get a database of horse racing using Pandas
I tried to make a todo application using bottle with python
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
I tried using parameterized
I tried using argparse
I tried hundreds of millions of SQLite with python
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using mecab with python2.7, ruby2.3, php7
I tried using ESPCN
I tried using openpyxl
I tried using Ipython
I tried using Pythonect, a dataflow programming language.
I tried using PyCaret
I tried reading a CSV file using Python
I tried using cron
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using doctest
I tried using jinja2
I tried using folium
I tried using time-window
I tried to make a ○ ✕ game using TensorFlow
I tried to create a table only with Django
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
[Python] I tried running a local server using flask
I tried drawing a pseudo fractal figure using Python
I tried collecting data from a website with Scrapy
I tried reading data from a file using Node.js.
I tried a simple RPA for login with selenium
I can't exe a project using PyWebView with PyInstaller
I tried using Python (3) instead of a scientific calculator
PyTorch Learning Note 2 (I tried using a pre-trained model)
I tried to draw a configuration diagram using Diagrams
I tried fp-growth with python
I tried Learning-to-Rank with Elasticsearch!
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried clustering with PyCaret
I tried using BigQuery ML
I tried using Amazon Glacier
Using a printer with Debian 10
I tried using git inspector
[Python] I tried using OpenPose