QR CookingNotes

CookingNotes

Your Personal Recipe Book

Get it on Google Play
QR FiNoteMe

FiNoteMe

Smart Finance Tracker

Get it on Google Play
programming Store and manage data in your apps but feel intimidated by databases? No worries! In this tutorial, we’ll use Python and SQLite to build a simple database app. Step by step, we’ll learn how to create tables, insert data, read it, update it, and delete it. No jargon, just clear explanations for beginners.

Beginner’s Guide: Using Python with SQLite to Build a Simple Database App

5 Min Read Verified Content

Step 0: Why Python + SQLite?

  • Python is easy to read and write—perfect for beginners. 🐍

  • SQLite is lightweight and doesn’t require a server—everything is stored in one file.

  • Together, they let you experiment and learn databases without complicated setup.

Think of it as having a notebook (SQLite) and a friendly pen (Python) to write, read, and manage your data. ✏️📒


Step 1: Install Python and SQLite

  1. Make sure Python is installed:

python --version
  1. SQLite comes built into Python through the sqlite3 module—so no extra install needed!



Step 2: Create a Python Script

Create a new file called app.py:

import sqlite3 # Connect to the database (or create it if it doesn't exist) conn = sqlite3.connect('my_database.db') # Create a cursor to execute SQL commands cursor = conn.cursor() print("Database connected successfully!")

Explanation for beginners:

  • conn → represents the database file

  • cursor → allows you to run SQL commands

  • This is like opening your notebook and preparing your pen



Step 3: Create a Table

We’ll make a simple table to store users:

cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL ) ''') print("Users table created successfully!")
  • IF NOT EXISTS → ensures no error if table already exists

  • AUTOINCREMENT → automatically numbers each user

  • NOT NULL → ensures every user has a name and email



Step 4: Insert Data

Let’s add a few users:

users = [ ('Alice', 'alice@example.com'), ('Bob', 'bob@example.com'), ('Charlie', 'charlie@example.com') ] cursor.executemany('INSERT INTO users (name, email) VALUES (?, ?)', users) conn.commit() # Save changes print("Users added successfully!")

Explanation:

  • ? are placeholders for safe insertion

  • executemany() lets you insert multiple records at once

  • conn.commit() saves changes to the database



Step 5: Read Data

Time to see what we have:

cursor.execute('SELECT * FROM users') rows = cursor.fetchall() for row in rows: print(f"{row[0]}: {row[1]} ({row[2]})")

Output might look like:

1: Alice (alice@example.com) 2: Bob (bob@example.com) 3: Charlie (charlie@example.com)

Explanation:

  • fetchall() returns all results as a list of tuples

  • Each row represents a single record



Step 6: Update Data

Suppose we want to change Bob’s email:

cursor.execute('UPDATE users SET email = ? WHERE name = ?', ('bob123@example.com', 'Bob')) conn.commit() print("Bob's email updated!")

Explanation:

  • SQL UPDATE modifies existing data

  • Always commit after changes



Step 7: Delete Data

Let’s remove Alice:

cursor.execute('DELETE FROM users WHERE name = ?', ('Alice',)) conn.commit() print("Alice deleted from the database!")

Step 8: Close the Connection

After all operations, always close the connection:

conn.close() print("Database connection closed!")


Step 9: Wrap-Up

Congratulations! 🎉 You’ve learned how to:

  1. Connect Python to SQLite

  2. Create a table

  3. Insert, read, update, and delete data

You now have the basic knowledge to build mini database applications, like a To-Do app, a contact manager, or any beginner project you like.

Advertisement
Back to Programming