In this video I’ll show you how to use the PostGres database with your Kivy apps with Python.

Postgresql is a very popular database for production level apps, and using it with Kivy is a breeze. In this video we’ll host our Postgres database on Heroku using their free tier and connect it to our local Kivy app.

#kivy #codemy

Python Code: postgres_db.py
GitHub Code: postgres_db.py

from kivy.lang import Builder
from kivymd.app import MDApp
#import sqlite3
import mysql.connector



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')
		
		# Define DB Stuff
		mydb = mysql.connector.connect(
			host = "localhost", 
			user = "root",
			passwd = "password123",
			database = "second_db",
			)

		# Create A Cursor
		c = mydb.cursor()

		# Create an actual database
		c.execute("CREATE DATABASE IF NOT EXISTS second_db")

		# Check to see if database was created
		#c.execute("SHOW DATABASES")
		#for db in c:
		#	print(db)



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

		# Check to see if table created
		#c.execute("SELECT * FROM customers")
		#print(c.description)



		# Commit our changes
		mydb.commit()

		# Close our connection
		mydb.close()

		return Builder.load_file('second_db.kv')



	def submit(self):
		# Create Database Or Connect To One
		#conn = sqlite3.connect('first_db.db')
		mydb = mysql.connector.connect(
			host = "localhost", 
			user = "root",
			passwd = "password123",
			database = "second_db",
			)

		# Create A Cursor
		c = mydb.cursor()

		

		# Add A Record
		sql_command = "INSERT INTO customers (name) VALUES (%s)"
		values = (self.root.ids.word_input.text,)
		
		# Execute SQL Command
		c.execute(sql_command, values)	
		

		# 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
		mydb.commit()

		# Close our connection
		mydb.close()

		

	def show_records(self):
		# Create Database Or Connect To One
		#conn = sqlite3.connect('first_db.db')
		mydb = mysql.connector.connect(
			host = "localhost", 
			user = "root",
			passwd = "password123",
			database = "second_db",
			)

		# Create A Cursor
		c = mydb.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
		mydb.commit()

		# Close our connection
		mydb.close()



MainApp().run()

Kivy Design Code: postgres_db.kv
GitHub Code: postgres_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

2 comments

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

  • Man, I am just starting a personal project where I want to connect a kivy app to an online postgres database. I am completely new with postgres and heroku and I thought it would be very challenging but wow, your 12mn video gives me everything I need and shows me that it is very easy. Thanks a lot

John Elder