76 lines
2.5 KiB
Python
76 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
检查car_owners表的结构,确认query_history_id字段是否正确添加
|
|
"""
|
|
|
|
import sqlite3
|
|
import logging
|
|
|
|
# 配置日志
|
|
handlers = [logging.FileHandler('db_check.log'), logging.StreamHandler()]
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
handlers=handlers
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 数据库文件路径
|
|
db_path = 'car_info.db'
|
|
|
|
def main():
|
|
"""
|
|
主函数:连接数据库并检查car_owners表的结构
|
|
"""
|
|
logger.info("开始检查car_owners表的结构")
|
|
|
|
try:
|
|
# 连接数据库
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# 检查car_owners表的结构
|
|
cursor.execute("PRAGMA table_info(car_owners)")
|
|
columns = cursor.fetchall()
|
|
|
|
logger.info("car_owners表的字段信息:")
|
|
for column in columns:
|
|
logger.info(f"字段ID: {column[0]}, 字段名: {column[1]}, 数据类型: {column[2]}, 非空约束: {column[3]}, 默认值: {column[4]}, 主键: {column[5]}")
|
|
|
|
# 检查外键约束
|
|
cursor.execute("PRAGMA foreign_key_list(car_owners)")
|
|
foreign_keys = cursor.fetchall()
|
|
|
|
logger.info("car_owners表的外键约束:")
|
|
for fk in foreign_keys:
|
|
logger.info(f"ID: {fk[0]}, 列名: {fk[3]}, 引用表: {fk[2]}, 引用列: {fk[4]}")
|
|
|
|
# 检查query_history_id字段是否存在
|
|
has_query_history_id = any(column[1] == 'query_history_id' for column in columns)
|
|
|
|
if has_query_history_id:
|
|
logger.info("car_owners表已成功添加query_history_id字段")
|
|
else:
|
|
logger.warning("car_owners表中不存在query_history_id字段")
|
|
|
|
# 检查query_history表是否存在
|
|
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='query_history'")
|
|
query_history_exists = cursor.fetchone() is not None
|
|
|
|
if query_history_exists:
|
|
logger.info("query_history表存在")
|
|
else:
|
|
logger.error("query_history表不存在,外键约束无法建立")
|
|
|
|
# 关闭数据库连接
|
|
conn.close()
|
|
logger.info("数据库连接已关闭,检查完成")
|
|
|
|
except sqlite3.Error as e:
|
|
logger.error(f"数据库操作失败: {str(e)}")
|
|
except Exception as e:
|
|
logger.error(f"程序执行出错: {str(e)}")
|
|
|
|
if __name__ == "__main__":
|
|
main() |