|
|
@@ -0,0 +1,32 @@
|
|
|
+# data.py
|
|
|
+
|
|
|
+import sqlite3
|
|
|
+
|
|
|
+# 创建和初始化数据库
|
|
|
+def initialize_db():
|
|
|
+ conn = sqlite3.connect('example.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute('''CREATE TABLE IF NOT EXISTS users (
|
|
|
+ id INTEGER PRIMARY KEY,
|
|
|
+ name TEXT,
|
|
|
+ age INTEGER
|
|
|
+ )''')
|
|
|
+ conn.commit()
|
|
|
+ conn.close()
|
|
|
+
|
|
|
+# 插入用户数据
|
|
|
+def insert_user(name, age):
|
|
|
+ conn = sqlite3.connect('example.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", (name, age))
|
|
|
+ conn.commit()
|
|
|
+ conn.close()
|
|
|
+
|
|
|
+# 查询所有用户数据
|
|
|
+def fetch_users():
|
|
|
+ conn = sqlite3.connect('example.db')
|
|
|
+ cursor = conn.cursor()
|
|
|
+ cursor.execute("SELECT * FROM users")
|
|
|
+ rows = cursor.fetchall()
|
|
|
+ conn.close()
|
|
|
+ return rows
|