Python SQLite Database Creation Guide
In Python, you can use built-in database modules like SQLite to create databases. Here is a simple example code to demonstrate how to create an SQLite database using Python.
import sqlite3
# 连接到 SQLite 数据库(如果不存在则创建一个)
conn = sqlite3.connect('example.db')
# 创建一个游标对象
cursor = conn.cursor()
# 创建一个名为 'users' 的表
cursor.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
)
''')
# 提交更改并关闭连接
conn.commit()
conn.close()
print("数据库创建成功!")
In this example, we first imported the sqlite3 module, then connected to an SQLite database named example.db. Next, we created a table called users and specified its structure. Finally, we committed the changes and closed the connection.
You can modify the structure and name of the table according to your own needs to create different database tables.