Back to Python
2026-04-095 min read

DS Database Table (Python Programming)

Learn DS Database Table (Python Programming) step by step with clear examples and exercises.

Title: Python Database Tables: A full guide for Data Science

Why This Matters

Data science relies heavily on databases to store, manage, and analyze large amounts of data. In Python, we can interact with databases using various libraries like SQLite, MySQL, PostgreSQL, and more. Today, we will focus on creating and manipulating database tables using the SQLite library. This skill is crucial for handling structured data in Python, making it a valuable asset in data science projects.

Importance of Database Tables in Data Science

  • Structured storage: Databases provide a structured way to store data, allowing easy retrieval and manipulation.
  • Scalability: Databases can handle large amounts of data and are designed for efficient querying.
  • Data integrity: Databases ensure data consistency and accuracy through constraints and transactions.

Prerequisites

Before diving into database tables, you should have a basic understanding of:

  • Python programming
  • Basic SQL syntax (SELECT, INSERT, UPDATE, DELETE)
  • How to install and import libraries in Python
  • Familiarity with data structures like lists and dictionaries

Understanding SQLite

SQLite is a C library that provides a lightweight SQL database for small to medium-sized applications. It doesn't require a separate server process, making it easy to use within Python.

Core Concept

To work with databases in Python, we'll use the sqlite3 library. First, let's create a new database file:

import sqlite3
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()

Now that we have a connection to our database, we can create a table using the CREATE TABLE SQL statement:

Create a new table called 'students' with columns 'id', 'name', and 'age'

cursor.execute('''CREATE TABLE students (

id INTEGER PRIMARY KEY AUTOINCREMENT,

name TEXT NOT NULL,

age INTEGER);''')


Notice that we added `AUTOINCREMENT` to the `id` column, which automatically assigns unique IDs for each new student.

To insert data into the table, we can use the `INSERT INTO` statement:

Insert a new student with name 'John Doe' and age 25

cursor.execute("INSERT INTO students (name, age) VALUES ('John Doe', 25);")

conn.commit() # Save the changes to the database


We can also retrieve data from the table using the `SELECT` statement:

Get all students from the 'students' table

cursor.execute("SELECT * FROM students;")

rows = cursor.fetchall()

for row in rows:

print(row)


Updating and deleting data follows similar patterns using `UPDATE` and `DELETE`. You can find more details about these operations in the [official SQLite documentation](https://www.sqlite.org/lang_corefunctions.html).

### Advanced Queries

SQLite supports advanced queries like `JOIN`, `GROUP BY`, and `HAVING` to perform complex data analysis. You can learn more about these functions in the [official SQLite documentation](https://www.sqlite.org/lang_expr.html).

Worked Example

Let's create a simple application that allows us to add, retrieve, update, and delete students from our database:

import sqlite3
import os

def create_table():
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER);''')
conn.commit()
conn.close()

def insert_student(name, age):
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
cursor.execute("INSERT INTO students (name, age) VALUES (?, ?);", (name, age))
conn.commit()
conn.close()

def get_students():
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM students;")
rows = cursor.fetchall()
return rows

def update_student(id, name, age):
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
cursor.execute("UPDATE students SET name=?, age=? WHERE id=?;", (name, age, id))
conn.commit()
conn.close()

def delete_student(id):
conn = sqlite3.connect('my_database.db')
cursor = conn.cursor()
cursor.execute("DELETE FROM students WHERE id=?;", (id,))
conn.commit()
conn.close()

if not os.path.exists('my_database.db'):
create_table()
insert_student('Alice', 23)
students = get_students()
print("Students:")
for student in students:
print(student)
update_student(1, 'John Doe', 25)
delete_student(1)
new_students = get_students()
print("\nUpdated Students:")
for student in new_students:
print(student)

Common Mistakes

  1. Forgetting to commit changes: Always call conn.commit() after making changes to the database to save them permanently.
  2. Not closing the connection: Remember to close the database connection after using it to free up resources.
  3. Incorrect SQL syntax: Make sure you understand the correct syntax for each SQL statement and avoid common mistakes like missing commas or parentheses.
  4. Not handling errors: Always check for errors when working with databases and handle them appropriately, such as by displaying an error message or logging the issue.
  5. Not using parameterized queries: Use parameterized queries to prevent SQL injection attacks and improve performance.

Common Error Handling

  • Using try and except blocks to catch exceptions when working with databases.
  • Displaying user-friendly error messages instead of raising unhandled exceptions.

Practice Questions

  1. Write a function to delete all students from the 'students' table in your database.
  2. Modify the insert_student function to accept multiple students at once as a list of tuples, where each tuple represents a student with name and age.
  3. Create a new table called 'courses' with columns 'id', 'name', and 'teacher'. Add some sample data to the table.
  4. Write a function to find the average age of all students in the 'students' table.
  5. Implement a search function that allows users to search for students by name.
  6. Create a function to count the number of students in the 'students' table.
  7. Implement a function to sort the 'students' table by age in descending order.
  8. Write a function to find the oldest student in the 'students' table.
  9. Modify the application to handle errors when inserting, updating, or deleting students.
  10. Add support for transactions to improve data consistency.

FAQ

  1. Why do I need to close the database connection after using it?

Closing the connection frees up resources and prevents memory leaks.

  1. What is SQL injection, and how can I prevent it?

SQL injection is a security vulnerability that allows attackers to inject malicious SQL code into your application. To prevent SQL injection, use parameterized queries instead of concatenating user input directly into SQL statements.

  1. Can I use other databases besides SQLite with Python?

Yes! Python supports various databases like MySQL, PostgreSQL, and MongoDB through their respective libraries. You can find more information about these libraries in the official Python documentation.

  1. How do I optimize database performance?

Optimizing database performance involves proper indexing, query optimization, and careful management of transactions and concurrent connections. You can find more information on these topics in the official SQLite documentation.

DS Database Table (Python Programming) | Python | XQA Learn