kivy
  1. kivy-working-with-databases

Working with Databases - (Kivy Storage and Database)

Kivy storage and database provides developers with a way to store and access data in applications. The Kivy framework comes with a storage API, which allows the data to be saved in a variety of formats, including JSON, CSV, XML, and more. It also provides a way to use SQLite, the most commonly used Relational Database Management System (RDBMS), within the Kivy framework.

Storing Data

The Kivy storage API provides the Storage class, which has a simple API for reading and writing data to files.

Syntax

from kivy.storage.jsonstore import JsonStore

store = JsonStore('filename.json')
store.put('key', value=value)
store.get('key')
store.delete('key')

Example

from kivy.storage.jsonstore import JsonStore

# Creating a JsonStore object
store = JsonStore('filename.json')

# Storing data
store.put('name', first='John', last='Doe')
store.put('age', value=30)

# Retrieving data
name = store.get('name')['first']
age = store.get('age')['value']

print("Name:", name, " | Age:", age)

# Deleting data
store.delete('age')

Output

Name: John  | Age: 30

Explanation

In the above example, we are creating a JsonStore object and saving data using the put method. The put method takes two arguments: a key to identify the data and the value to store. We are retrieving the data using the get method and deleting the data using the delete method.

Using SQLite Database

Syntax

from kivy.storage.sqlite import SQLiteConnection

conn = SQLiteConnection('dbname')

# Retrieving the cursor
cursor = conn.cursor()

# Executing SQL statement
cursor.execute('SQL STATEMENT')

# Closing cursor and connection
cursor.close()
conn.close()

Example

import os
from kivy.storage.sqlite import SQLiteConnection

if os.path.exists('test.db'):
    os.remove('test.db')

conn = SQLiteConnection('test.db')
cursor = conn.cursor()

# Creating table
cursor.execute('''CREATE TABLE users
                  (name TEXT, age INT)''')

# Inserting data
cursor.execute("INSERT INTO users VALUES ('John Doe', 25)")
cursor.execute("INSERT INTO users VALUES ('Jane Doe', 30)")

# Retrieving data
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()

for row in rows:
    print(row)

cursor.close()
conn.close()

Output

('John Doe', 25)
('Jane Doe', 30)

Explanation

In this example, we are creating a SQLiteConnection object and retrieving the cursor to execute various SQL statements. We are creating a new table called users and inserting two rows of data into it. Finally, we are retrieving the data and printing it.

Use

The Kivy storage and database API can be used to store and retrieve data from persistent storage, such as files or databases. This makes it ideal for saving user preferences, game progress, or any other kind of data your application needs to store.

Important Points

  • Kivy storage API provides a simple way to read and write data to files.
  • SQLite is a commonly used RDBMS with which Kivy has integrated.
  • Data can be stored in various formats like JSON, CSV, XML, etc.

Summary

In this tutorial, we learned about the Kivy storage and database API. We saw how to use the storage API to store and retrieve data from files and how to use SQLite within Kivy to store data in a database. We also saw some important points regarding the use of the Kivy storage API.

Published on: