46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
|
|
from sql_utils import SQLUtils
|
||
|
|
import datetime
|
||
|
|
|
||
|
|
def init_database():
|
||
|
|
db = SQLUtils('sqlite', database='db/jtDB.db')
|
||
|
|
|
||
|
|
# 创建用户表
|
||
|
|
create_table_sql = """
|
||
|
|
CREATE TABLE IF NOT EXISTS user (
|
||
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
|
username VARCHAR(50) NOT NULL UNIQUE,
|
||
|
|
password VARCHAR(100) NOT NULL,
|
||
|
|
create_time TIMESTAMP NOT NULL,
|
||
|
|
create_by VARCHAR(50) NOT NULL,
|
||
|
|
update_time TIMESTAMP,
|
||
|
|
update_by VARCHAR(50),
|
||
|
|
is_deleted BOOLEAN DEFAULT FALSE
|
||
|
|
);
|
||
|
|
"""
|
||
|
|
|
||
|
|
# 获取当前时间
|
||
|
|
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||
|
|
|
||
|
|
# 插入系统用户
|
||
|
|
insert_system_user_sql = """
|
||
|
|
INSERT OR IGNORE INTO user (
|
||
|
|
username, password, create_time, create_by, is_deleted
|
||
|
|
) VALUES (
|
||
|
|
'system', '123456', ?, 'system', FALSE
|
||
|
|
);
|
||
|
|
"""
|
||
|
|
|
||
|
|
try:
|
||
|
|
db.begin_transaction()
|
||
|
|
db.execute_query(create_table_sql)
|
||
|
|
db.execute_query(insert_system_user_sql, (current_time,))
|
||
|
|
db.commit_transaction()
|
||
|
|
print("Database initialized successfully!")
|
||
|
|
except Exception as e:
|
||
|
|
db.rollback_transaction()
|
||
|
|
print(f"Error initializing database: {str(e)}")
|
||
|
|
finally:
|
||
|
|
db.close()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
init_database()
|