In this video I’ll show you how to start using the SQLite3 Database with your Kivy Apps!

We’ll start by learning how to add SQLite3, the built in database that comes with python to your Kivy app.

We’ll create a very basic app that lets us add a name to the database, and then show all the database records on the screen.

#kivy #codemy

Python Code: first_db.py
GitHub Code: first_db.py

from kivy.lang import Builder
from kivymd.app import MDApp
import sqlite3

class MainApp(MDApp):
	def build(self):
		self.theme_cls.theme_style = "Dark"
		self.theme_cls.primary_palette = "BlueGray"

		# Create Database Or Connect To One
		conn = sqlite3.connect('first_db.db')

		# Create A Cursor
		c = conn.cursor()

		# Create A Table
		c.execute("""CREATE TABLE if not exists customers(
			name text)
		 """)

		# Commit our changes
		conn.commit()

		# Close our connection
		conn.close()

		return Builder.load_file('first_db.kv')



	def submit(self):
		# Create Database Or Connect To One
		conn = sqlite3.connect('first_db.db')

		# Create A Cursor
		c = conn.cursor()

		# Add A Record
		c.execute("INSERT INTO customers VALUES (:first)",
			{
				'first': self.root.ids.word_input.text,
			})

		# Add a little message
		self.root.ids.word_label.text = f'{self.root.ids.word_input.text} Added'

		# Clear the input box
		
		self.root.ids.word_input.text = ''


		# Commit our changes
		conn.commit()

		# Close our connection
		conn.close()

		

	def show_records(self):
		# Create Database Or Connect To One
		conn = sqlite3.connect('first_db.db')

		# Create A Cursor
		c = conn.cursor()

		# Grab records from database
		c.execute("SELECT * FROM customers")
		records = c.fetchall()

		word = ''
		# Loop thru records
		for record in records:
			word = f'{word}\n{record[0]}'
			self.root.ids.word_label.text = f'{word}'

		# Commit our changes
		conn.commit()

		# Close our connection
		conn.close()


MainApp().run()

Kivy Design Code: first_db.kv
GitHub Code: first_db.kv

MDFloatLayout:
	BoxLayout:
		orientation: "vertical"
		size: root.width, root.height
	
		Label:
			id: word_label
			text_size: self.size
			halign: "center"
			valign: "middle"
			text: "Enter A Name"
			font_size: 32

		TextInput:
			id: word_input
			multiline: False
			size_hint: (1, .5)

		Button:
			size_hint: (1, .5)
			font_size: 32
			text: "Submit Name"
			on_press: app.submit()

		Button:
			size_hint: (1, .5)
			font_size: 32
			text: "Show Records"
			on_press: app.show_records()

			

John Elder

John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet's earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that's been used by over 3 million individuals, businesses, and governments in over 42 countries. He's written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.

View all posts

Add comment

Your email address will not be published. Required fields are marked *

John Elder