Query into pandas

Python Practice › SQLite Programs › Program 20

Query into pandas

EasySQLite Programs · 20 of 20

Write a Python program to load a query result into a pandas DataFrame.

Output
    city  total
0   Pune   2180
1  Delhi    450

Approach

  1. pd.read_sql_query(sql, conn).

Solution

Python
import sqlite3import pandas as pd conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE sales (city TEXT, amount INTEGER)")conn.executemany("INSERT INTO sales VALUES (?, ?)", [("Pune", 1200), ("Delhi", 450), ("Pune", 980)])df = pd.read_sql_query("SELECT city, SUM(amount) AS total FROM sales GROUP BY city ORDER BY total DESC", conn)print(df)
Output
    city  total
0   Pune   2180
1  Delhi    450

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Python SQLite Programs with Solutions

Python Practice › SQLite Programs

Python SQLite Programs with Solutions

20 programs19 easy1 medium0 hard

20 SQLite programs using Python’s built-in sqlite3 module: creating tables, inserting with parameters, querying, updating, deleting, joins, aggregates, transactions and exporting to CSV. Most use an in-memory database so they run anywhere.

Before you start

Read the SQLite lesson first if the topic is new to you.

All programs

Export a table to CSV

Python Practice › SQLite Programs › Program 18

Export a table to CSV

EasySQLite Programs · 18 of 20

Write a Python program to export a table to a CSV file.

Output
name,marks
Asha,91
Ravi,74

Approach

  1. Read rows and column names, then use csv.writer.

Solution

Python
import csvimport sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE students (name TEXT, marks INTEGER)")conn.executemany("INSERT INTO students VALUES (?, ?)", [("Asha", 91), ("Ravi", 74)])cur = conn.execute("SELECT * FROM students")with open("students.csv", "w", newline="") as f:    writer = csv.writer(f)    writer.writerow([c[0] for c in cur.description])    writer.writerows(cur)print(open("students.csv").read())
Output
name,marks
Asha,91
Ravi,74

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Why parameters prevent SQL injection

Python Practice › SQLite Programs › Program 19

Why parameters prevent SQL injection

EasySQLite Programs · 19 of 20

Write a Python program that shows how an f-string query is unsafe and a parameter is safe.

Output
unsafe query returned: [('asha',), ('ravi',)]
safe query returned: []

Approach

  1. Use a malicious input with both styles.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE users (name TEXT, secret TEXT)")conn.executemany("INSERT INTO users VALUES (?, ?)", [("asha", "s1"), ("ravi", "s2")]) user_input = "nobody' OR '1'='1"unsafe = conn.execute(f"SELECT name FROM users WHERE name = '{user_input}'").fetchall()safe = conn.execute("SELECT name FROM users WHERE name = ?", (user_input,)).fetchall()print("unsafe query returned:", unsafe)print("safe query returned:", safe)
Output
unsafe query returned: [('asha',), ('ravi',)]
safe query returned: []

Line by line

Line 7

The input closes the quote and adds OR '1'='1, so the unsafe query returns every user.

Line 8

With a parameter the whole input is treated as a name, which matches nobody.

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Search with LIKE

Python Practice › SQLite Programs › Program 16

Search with LIKE

EasySQLite Programs · 16 of 20

Write a Python program to find products whose name contains a word.

Output
[('USB cable',), ('USB hub',)]

Approach

  1. LIKE '%' || ? || '%' keeps the value as a parameter.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE products (name TEXT)")conn.executemany("INSERT INTO products VALUES (?)", [("USB cable",), ("Wireless mouse",), ("USB hub",), ("Keyboard",)])term = "usb"print(conn.execute("SELECT name FROM products WHERE name LIKE '%' || ? || '%'", (term,)).fetchall())
Output
[('USB cable',), ('USB hub',)]

Line by line

Line 6

SQLite’s LIKE ignores case for English letters.

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Top N rows

Python Practice › SQLite Programs › Program 17

Top N rows

EasySQLite Programs · 17 of 20

Write a Python program to get the top 3 scores.

Output
[('Gill', 90), ('Virat', 82), ('Rahul', 61)]

Approach

  1. ORDER BY ... DESC LIMIT 3.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE scores (player TEXT, runs INTEGER)")conn.executemany("INSERT INTO scores VALUES (?, ?)", [("Virat", 82), ("Rohit", 45), ("Gill", 90), ("Rahul", 61)])print(conn.execute("SELECT player, runs FROM scores ORDER BY runs DESC LIMIT 3").fetchall())
Output
[('Gill', 90), ('Virat', 82), ('Rahul', 61)]

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Column names of a query

Python Practice › SQLite Programs › Program 15

Column names of a query

EasySQLite Programs · 15 of 20

Write a Python program to print the column names of a query result.

Output
['id', 'name', 'email']

Approach

  1. cursor.description.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE users (id INTEGER, name TEXT, email TEXT)")cur = conn.execute("SELECT * FROM users")print([col[0] for col in cur.description])
Output
['id', 'name', 'email']

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Use a database file

Python Practice › SQLite Programs › Program 14

Use a database file

EasySQLite Programs · 14 of 20

Write a Python program that saves data to a .db file and reads it back with a new connection.

Output
[('pen', 10.0)] True

Approach

  1. Connect to a file name; close and reconnect.

Solution

Python
import osimport sqlite3 if os.path.exists("shop.db"):    os.remove("shop.db")with sqlite3.connect("shop.db") as conn:    conn.execute("CREATE TABLE items (name TEXT, price REAL)")    conn.execute("INSERT INTO items VALUES ('pen', 10)")conn.close() conn = sqlite3.connect("shop.db")print(conn.execute("SELECT * FROM items").fetchall(), os.path.getsize("shop.db") > 0)conn.close()
Output
[('pen', 10.0)] True

Line by line

Lines 6-8

The with block commits the insert; close() releases the file.

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Customers without orders

Python Practice › SQLite Programs › Program 12

Customers without orders

EasySQLite Programs · 12 of 20

Write a Python program to find customers who have not placed any order.

Output
[('Meena',), ('Ravi',)]

Approach

  1. LEFT JOIN and check for NULL.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.executescript("""    CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT);    CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER);    INSERT INTO customers VALUES (1, 'Asha'), (2, 'Ravi'), (3, 'Meena');    INSERT INTO orders VALUES (10, 1), (11, 1);""")print(conn.execute("""SELECT c.name FROM customers c LEFT JOIN orders o ON o.customer_id = c.id                      WHERE o.id IS NULL ORDER BY c.name""").fetchall())
Output
[('Meena',), ('Ravi',)]

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Transaction with rollback

Python Practice › SQLite Programs › Program 13

Transaction with rollback

EasySQLite Programs · 13 of 20

Write a Python program that transfers money between accounts and rolls back on error.

Output
done [('Asha', 300), ('Ravi', 300)]
failed, rolled back [('Asha', 300), ('Ravi', 300)]

Approach

  1. with conn: commits on success and rolls back on an exception.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.execute("CREATE TABLE acc (name TEXT PRIMARY KEY, balance INTEGER CHECK (balance >= 0))")conn.executemany("INSERT INTO acc VALUES (?, ?)", [("Asha", 500), ("Ravi", 100)])conn.commit() def transfer(src, dst, amount):    try:        with conn:            conn.execute("UPDATE acc SET balance = balance + ? WHERE name = ?", (amount, dst))            conn.execute("UPDATE acc SET balance = balance - ? WHERE name = ?", (amount, src))        return "done"    except sqlite3.IntegrityError:        return "failed, rolled back" print(transfer("Asha", "Ravi", 200), conn.execute("SELECT * FROM acc").fetchall())print(transfer("Ravi", "Asha", 1000), conn.execute("SELECT * FROM acc").fetchall())
Output
done [('Asha', 300), ('Ravi', 300)]
failed, rolled back [('Asha', 300), ('Ravi', 300)]

Line by line

Line 4

The CHECK constraint makes a negative balance an error.

Lines 11-12

If the second update fails, the first one is undone too.

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs