20250819corp

This commit is contained in:
2025-09-19 16:47:15 +08:00
parent fc5fb355b4
commit b0e8a68204
12 changed files with 1148 additions and 809 deletions
+68 -7
View File
@@ -1,10 +1,13 @@
import sqlite3
import datetime
# 导入数据库工具
from db_utils import get_db_connection, close_db_connection
# 连接到数据库并确保users表结构正确
def update_users_table():
try:
# 连接数据库
conn = sqlite3.connect('car_info.db')
conn = get_db_connection()
cursor = conn.cursor()
# 检查users表的当前结构
@@ -42,7 +45,10 @@ def update_users_table():
print("已更新admin用户信息")
# 提交更改
conn.commit()
try:
conn.commit()
except Exception as e:
print(f"提交更改时出错: {e}")
# 再次验证表结构
cursor.execute("PRAGMA table_info(users)")
@@ -59,14 +65,69 @@ def update_users_table():
print(f"ID: {admin_user[0]}, 用户名: {admin_user[1]}, 姓名: {admin_user[2]}, 电话: {admin_user[3]}, 部门: {admin_user[4]}")
# 关闭连接
conn.close()
close_db_connection(conn)
print("\n数据库更新完成")
except Exception as e:
print(f"更新数据库时出错: {str(e)}")
# 确保在异常情况下也关闭连接
if 'conn' in locals():
conn.close()
if 'conn' in locals() and conn:
close_db_connection(conn)
# 更新数据库中的数据
def update_database():
try:
# 连接数据库
conn = get_db_connection()
cursor = conn.cursor()
# 检查是否存在测试数据表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='test_data'")
if not cursor.fetchone():
# 如果不存在,创建测试数据表
cursor.execute('''
CREATE TABLE test_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
value TEXT,
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
print("测试数据表创建成功")
# 检查是否有测试数据,如果没有则插入
cursor.execute("SELECT COUNT(*) FROM test_data")
count = cursor.fetchone()[0]
if count == 0:
# 插入测试数据
test_data = [
('测试数据1', 'value1', datetime.datetime.now()),
('测试数据2', 'value2', datetime.datetime.now()),
('测试数据3', 'value3', datetime.datetime.now())
]
for data in test_data:
cursor.execute(
"INSERT INTO test_data (name, value, create_time, update_time) VALUES (?, ?, ?, ?)",
data
)
conn.commit()
print("测试数据插入成功")
# 关闭数据库连接
close_db_connection(conn)
return True, "数据库更新成功"
except Exception as e:
# 确保在异常情况下也关闭连接
if 'conn' in locals() and conn:
close_db_connection(conn)
return False, f"数据库更新失败:{str(e)}"
# 如果直接运行此脚本,则执行更新操作
if __name__ == "__main__":
update_users_table()
success, message = update_database()
print(message)