79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
检查car_owners表的约束条件
|
|
"""
|
|
|
|
import sqlite3
|
|
import logging
|
|
|
|
# 配置日志
|
|
handlers = [logging.FileHandler('constraints_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 check_car_owners_constraints():
|
|
"""
|
|
检查car_owners表的约束条件
|
|
"""
|
|
logger.info("开始检查car_owners表的约束条件")
|
|
|
|
try:
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
|
|
# 检查表的唯一约束
|
|
cursor.execute("PRAGMA index_list(car_owners)")
|
|
indexes = cursor.fetchall()
|
|
|
|
logger.info("car_owners表的索引列表:")
|
|
for index in indexes:
|
|
index_id, name, unique, origin, partial = index
|
|
logger.info(f"索引ID: {index_id}, 名称: {name}, 唯一: {unique}, 来源: {origin}, 部分索引: {partial}")
|
|
|
|
# 获取索引的列信息
|
|
if name:
|
|
cursor.execute(f"PRAGMA index_info({name})")
|
|
index_columns = cursor.fetchall()
|
|
logger.info(f"索引 {name} 的列信息:")
|
|
for col in index_columns:
|
|
col_id, seq_no, col_name = col
|
|
logger.info(f"列ID: {col_id}, 序列号: {seq_no}, 列名: {col_name}")
|
|
|
|
# 获取表的创建语句(如果可能)
|
|
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='car_owners'")
|
|
create_sql = cursor.fetchone()
|
|
|
|
if create_sql:
|
|
logger.info(f"car_owners表的创建语句: {create_sql[0]}")
|
|
else:
|
|
logger.warning("无法获取car_owners表的创建语句")
|
|
|
|
# 关闭数据库连接
|
|
conn.close()
|
|
|
|
except sqlite3.Error as e:
|
|
logger.error(f"数据库操作失败: {str(e)}")
|
|
except Exception as e:
|
|
logger.error(f"程序执行出错: {str(e)}")
|
|
|
|
def main():
|
|
"""
|
|
主函数
|
|
"""
|
|
logger.info("开始检查数据库约束")
|
|
|
|
# 检查car_owners表的约束条件
|
|
check_car_owners_constraints()
|
|
|
|
logger.info("数据库约束检查完成")
|
|
|
|
if __name__ == "__main__":
|
|
main() |