[100 days proficient in python] Day33: Use python to operate the database_SQLite database use and actual combat

Table of contents

 Column Guide 

1 Introduction to SQLite

2 SQLite database installation and use

2.1 Check for SQLite support

  2.2 Create database file      

2.2.1 Create a database file using Python:

2.2.2 Create a database file using the command line:

2.3 Connect to SQLite database:

3 SQL statements commonly used in SQLite 

3.1 Overview of SQL statements

 3.2 Commonly used SQL statements in SQLite

(1) Create a table:

(2) Insert data:

(3) Query data:

(4) Update data:

(5) Delete data:

(6) Filter data:

(7) Sorting data:

(8) Calculation statistics:

(9) Connection table:

(10) Packet data:

4 Operation SQLite

 5 SQLite in action

5.1 Practice 1 Create a simple task management application    

5.2 Actual Combat 2 Create a task management application, expand editing and deleting tasks

5.3 Actual Combat 3 Realize user data addition, deletion, viewing, modification, etc.


 Column Guide 

Column subscription address: https://blog.csdn.net/qq_35831906/category_12375510.html


1 Introduction to SQLite

        SQLite is an embedded relational database management system (RDBMS), known for its lightweight, efficient and easy to use. It is a self-contained, serverless database engine that stores the entire database in a single file. SQLite does not require an independent server process, but is directly embedded in the application, so it is suitable for various environments such as embedded systems, mobile applications, and desktop applications.

Following are some important features and advantages of SQLite:

  1. Lightweight and fast : SQLite is a lightweight database engine that doesn't take up much memory or resources. It is very fast when accessing and querying data, especially suitable for small projects and mobile applications.

  2. Zero configuration : Unlike other databases, SQLite does not require a separate server process and requires no complicated configuration. With just one file, you can start storing and querying data.

  3. Self-contained : SQLite databases are stored in a single file, which makes backing up, migrating, and sharing databases very simple.

  4. No need for special management : Since SQLite is embedded, it does not require an independent database administrator (DBA) to manage and maintain, reducing the burden of database management.

  5. Support multiple programming languages : SQLite supports multiple programming languages, including C/C++, Python, Java, C#, PHP, etc., so that developers can use them in the languages ​​they are familiar with.

  6. Transaction support : SQLite supports transaction processing to ensure data consistency and integrity. You can use transactions to perform a set of operations that either all succeed or all roll back.

  7. Open source and cross-platform : SQLite is open source and can run on different operating systems including Windows, macOS, Linux, etc.

Although SQLite is suitable for many scenarios, it is not suitable for large-scale, high-concurrency applications, because it does not support complex database operations such as multi-user simultaneous writing (write lock). However, in many lightweight and small to medium scale projects, SQLite provides a simple, fast, easy-to-use database solution.

SQLite database installation and use

        SQLite is an embedded database and usually does not need to be installed separately because it is already part of Python's standard library. This means that you can use SQLite directly in Python without installing additional software. Here's how to use SQLite from Python:

2.1 Check for SQLite support

         For the most part, Python already includes SQLite support by default. You can run the following code in the Python interactive environment to check whether SQLite is supported:

import sqlite3
print(sqlite3.sqlite_version)

The output is as follows, if the output shows the SQLite version number, then your Python supports SQLite.

  2.2 Create database file      

        In SQLite, the database file is automatically created the first time you connect to the database, if the file does not exist. Instead of manually creating a database file, you specify the name of the database file when connecting, and SQLite will create a new database file if the file does not exist.

2.2.1 Create a database file using Python :

You can use the following code to create a mydatabase.dbdatabase file named and connect to it:

import sqlite3

# 连接到 SQLite 数据库(如果不存在则会创建)
conn = sqlite3.connect('mydatabase.db')

# 关闭连接
conn.close()

     In this example, sqlite3.connect()the argument to the function is the name of the database file, ie 'mydatabase.db'. If the file does not exist, SQLite will automatically create a new database file.

        Note that the database file will be created in the same directory as your Python script. If you wish to save the database file in a specific directory, you need to provide the full path. When connecting to a database, SQLite will check if the specified path exists, and if not, create the corresponding database file.

        In short, you don't need to manually create the SQLite database file, you only need to specify the name or path of the database file when connecting.

2.2.2 Create a database file using the command line :

        If you want to create database files on the command line, you can use SQLite's command line tools. Open a terminal or command line window and execute the following command:

sqlite3 mydatabase.db

         This will open a SQLite command line session and create a mydatabase.dbdatabase file named . In a SQLite command line session, you can execute SQL statements to create tables, insert data, and more.

        Whether you are using Python or the command line, whenever you connect to a database file that does not exist, SQLite will automatically create the database file.

2.3 Connect to SQLite database :

         You can use sqlite3.connect()the function to connect to a SQLite database file. If the specified database file does not exist, it will be created. 

import sqlite3

# 连接到 SQLite 数据库(如果不存在则会创建)
conn = sqlite3.connect('mydatabase.db')

3 SQL statements commonly used in SQLite 

3.1 Overview of SQL statements

        SQL (Structured Query Language) is a standardized query language for managing relational databases. It is used to perform various database operations, including querying, inserting, updating, deleting of data, and defining the database structure. The following are commonly used SQL statements:

  1. DDL (Data Definition Language) : The data definition language is used to define and manage the structure of the database, including tables, columns, indexes, etc.

    • CREATE TABLE: Create a data table.
    • ALTER TABLE: Modify the data table structure.
    • DROP TABLE: Delete the data table.
    • CREATE INDEX: Create an index.
  2. DML (Data Manipulation Language) : Data manipulation language is used to perform data addition, deletion, modification and query operations.

    • SELECT:Query data.
    • INSERT INTO: Insert data.
    • UPDATE:update data.
    • DELETE FROM:delete data.
  3. DQL (Data Query Language) : Data Query Language is used to query and retrieve data.

    • SELECT: Used to retrieve data from the database.
  4. DCL (Data Control Language) : Data Control Language is used to manage database permissions and access control.

    • GRANT: Grants access to a user or role.
    • REVOKE: Revoke access for a user or role.
  5. TCL (Transaction Control Language) : The transaction control language is used to manage transaction operations in the database.

    • COMMIT: Commit the transaction.
    • ROLLBACK: Rollback the transaction.
    • SAVEPOINT: Set a savepoint for partial rollback within a transaction.

SQL statements are a key tool for interacting with databases. Whether creating database structures, performing data manipulation, or retrieving information from a database, SQL provides developers with powerful tools for managing and manipulating data. Different relational database systems may differ in some syntax and functionality, but the core syntax of SQL is generally common.

 3.2 Commonly used SQL statements in SQLite

SQLite supports standard SQL syntax, the following are some commonly used SQL statement examples in SQLite:

(1) Create a table :

Use CREATE TABLEthe statement to create a data table.

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL,
    email TEXT UNIQUE,
    age INTEGER
);
(2) Insert data :

Use INSERT INTOthe statement to insert data into the data table.

INSERT INTO users (username, email, age)
VALUES ('user1', '[email protected]', 25);
(3) Query data :

Use SELECTthe statement to query data.

SELECT * FROM users;
(4) Update data :

Use UPDATEthe statement to update data.

UPDATE users
SET age = 30
WHERE username = 'user1';
(5) Delete data :

Use DELETE FROMthe statement to delete data.

DELETE FROM users
WHERE username = 'user1';
(6) Filter data :

Use WHEREclauses to filter data.

SELECT * FROM users
WHERE age > 25;
(7) Sorting data :

Use ORDER BYthe clause to sort the data.

SELECT * FROM users
ORDER BY age DESC;
(8) Calculation statistics :

Use aggregate functions (eg COUNT, SUM, AVG) to compute statistics.

SELECT COUNT(*) FROM users;
SELECT AVG(age) FROM users;
(9) Connection table :

Use JOINthe statement to join multiple data tables.

SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
(10) Packet data :

Use GROUP BYclauses to group data.

SELECT gender, AVG(age) FROM users
GROUP BY gender;

4 Operation SQLite

        Operating an SQLite database involves steps such as creating connections, creating cursors, executing SQL statements, and processing data. The following is a simple example demonstrating how to use Python's sqlite3module to manipulate an SQLite database:

import sqlite3

# 连接到 SQLite 数据库(如果不存在则会创建)
conn = sqlite3.connect('mydatabase.db')

# 创建游标对象
cursor = conn.cursor()

# 创建数据表
create_table_query = '''
CREATE TABLE IF NOT EXISTS books (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    author TEXT,
    price REAL
)
'''
cursor.execute(create_table_query)

# 插入数据
insert_query = 'INSERT INTO books (title, author, price) VALUES (?, ?, ?)'
data = [('Book A', 'Author A', 19.99),
        ('Book B', 'Author B', 29.99),
        ('Book C', 'Author C', 9.99)]
cursor.executemany(insert_query, data)

# 提交事务
conn.commit()

# 查询数据
select_query = 'SELECT * FROM books'
cursor.execute(select_query)
rows = cursor.fetchall()
for row in rows:
    print(row)

# 关闭游标和连接
cursor.close()
conn.close()

output

 In this example, we first create a database connection and then create a cursor object. Next, we execute SQL statements to create data tables, insert data and query data. Finally, we commit the transaction and close the cursor and connection.

 5 SQLite in action

        SQLite has a good application in many lightweight and small to medium projects.

5.1 Practice 1 Create a simple task management application    

         The following is a SQLite practical example showing how to create a simple task management application, including functions such as creating tasks, listing tasks, and marking tasks as complete.

import sqlite3

# 连接到 SQLite 数据库(如果不存在则会创建)
conn = sqlite3.connect('task_manager.db')

# 创建游标对象
cursor = conn.cursor()

# 创建数据表
create_table_query = '''
CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    completed BOOLEAN
)
'''
cursor.execute(create_table_query)

def create_task(title, description):
    insert_query = 'INSERT INTO tasks (title, description, completed) VALUES (?, ?, ?)'
    cursor.execute(insert_query, (title, description, False))
    conn.commit()
    print("Task created successfully.")

def list_tasks():
    select_query = 'SELECT * FROM tasks'
    cursor.execute(select_query)
    tasks = cursor.fetchall()
    for task in tasks:
        print(f"Task ID: {task[0]}, Title: {task[1]}, Completed: {task[3]}")

def mark_task_completed(task_id):
    update_query = 'UPDATE tasks SET completed = ? WHERE id = ?'
    cursor.execute(update_query, (True, task_id))
    conn.commit()
    print("Task marked as completed.")

# 创建任务
create_task("Buy groceries", "Get milk, eggs, and bread")

# 列出任务
list_tasks()

# 标记任务为完成
task_id_to_complete = 1
mark_task_completed(task_id_to_complete)

# 列出更新后的任务列表
list_tasks()

# 关闭游标和连接
cursor.close()
conn.close()

output:

         In this example, we create a tasksdata table to store task information. We implemented three basic functions: creating tasks, listing tasks, and marking tasks as done.

        Make sure you have the module installed sqlite3, usually it's part of Python's standard library. In practical applications, you may also need to do more error handling, user interface design, etc. to meet actual needs.

5.2 Actual Combat 2 Create a task management application, expand editing and deleting tasks

        When it comes to editing and deleting tasks, we can add corresponding functions to achieve these operations. The following is a practical example of SQLite that extends the functionality of editing and deleting tasks:

import sqlite3

# 连接到 SQLite 数据库(如果不存在则会创建)
conn = sqlite3.connect('task_manager.db')

# 创建游标对象
cursor = conn.cursor()

# 创建数据表
create_table_query = '''
CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    completed BOOLEAN
)
'''
cursor.execute(create_table_query)

def create_task(title, description):
    insert_query = 'INSERT INTO tasks (title, description, completed) VALUES (?, ?, ?)'
    cursor.execute(insert_query, (title, description, False))
    conn.commit()
    print("Task created successfully.")

def list_tasks():
    select_query = 'SELECT * FROM tasks'
    cursor.execute(select_query)
    tasks = cursor.fetchall()
    for task in tasks:
        print(f"Task ID: {task[0]}, Title: {task[1]}, Completed: {task[3]}")

def mark_task_completed(task_id):
    update_query = 'UPDATE tasks SET completed = ? WHERE id = ?'
    cursor.execute(update_query, (True, task_id))
    conn.commit()
    print("Task marked as completed.")

def edit_task(task_id, new_title, new_description):
    update_query = 'UPDATE tasks SET title = ?, description = ? WHERE id = ?'
    cursor.execute(update_query, (new_title, new_description, task_id))
    conn.commit()
    print("Task edited successfully.")

def delete_task(task_id):
    delete_query = 'DELETE FROM tasks WHERE id = ?'
    cursor.execute(delete_query, (task_id,))
    conn.commit()
    print("Task deleted successfully.")

# 创建任务
create_task("Buy groceries", "Get milk, eggs, and bread")

# 编辑任务
edit_task(1, "Buy groceries", "Get milk, eggs, bread, and fruits")

# 列出任务
list_tasks()

# 删除任务
delete_task(1)

# 列出更新后的任务列表
list_tasks()

# 关闭游标和连接
cursor.close()
conn.close()

5.3 Actual Combat 3 Realize user data addition, deletion, viewing, modification, etc.

        The following is a case of using the SQLite database, showing how to implement operations such as adding user data, viewing user data, modifying user data, and deleting user data:

import sqlite3

# 连接到 SQLite 数据库(如果不存在则会创建)
conn = sqlite3.connect('user_database.db')

# 创建游标对象
cursor = conn.cursor()

# 创建数据表
create_table_query = '''
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    username TEXT NOT NULL,
    email TEXT NOT NULL,
    age INTEGER
)
'''
cursor.execute(create_table_query)

def create_user(username, email, age):
    insert_query = 'INSERT INTO users (username, email, age) VALUES (?, ?, ?)'
    cursor.execute(insert_query, (username, email, age))
    conn.commit()
    print("User created successfully.")

def list_users():
    select_query = 'SELECT * FROM users'
    cursor.execute(select_query)
    users = cursor.fetchall()
    for user in users:
        print(f"User ID: {user[0]}, Username: {user[1]}, Email: {user[2]}, Age: {user[3]}")

def edit_user(user_id, new_username, new_email, new_age):
    update_query = 'UPDATE users SET username = ?, email = ?, age = ? WHERE id = ?'
    cursor.execute(update_query, (new_username, new_email, new_age, user_id))
    conn.commit()
    print("User information edited successfully.")

def delete_user(user_id):
    delete_query = 'DELETE FROM users WHERE id = ?'
    cursor.execute(delete_query, (user_id,))
    conn.commit()
    print("User deleted successfully.")

# 创建用户
create_user("user1", "[email protected]", 25)

# 查看用户列表
list_users()

# 修改用户信息
edit_user(1, "updated_user1", "[email protected]", 30)

# 查看更新后的用户列表
list_users()

# 删除用户
delete_user(1)

# 查看删除后的用户列表
list_users()

# 关闭游标和连接
cursor.close()
conn.close()

output: 

 In this case, we created a usersdata table to store user information. We have implemented four basic functions: create user, view user list, edit user information and delete user. The realization of these functions all depends on the SQL INSERT, SELECT, UPDATEand DELETEstatements.

Guess you like

Origin blog.csdn.net/qq_35831906/article/details/132257274