import_data.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import sqlite3
  2. import csv
  3. import time
  4. import os
  5. # 记录开始时间
  6. start_time = time.time()
  7. # 连接数据库(动态获取数据库路径)
  8. db_path = os.path.abspath('../db.sqlite3')
  9. print(f"[{time.strftime('%H:%M:%S')}] 正在连接数据库: {db_path}")
  10. try:
  11. conn = sqlite3.connect(db_path)
  12. cursor = conn.cursor()
  13. # 在插入前清空表
  14. cursor.execute("DELETE FROM flowlist;")
  15. print(f"[{time.strftime('%H:%M:%S')}] ✅ 数据库连接成功")
  16. # 读取CSV文件
  17. csv_file = 'flow.csv'
  18. print(f"\n[{time.strftime('%H:%M:%S')}] 开始导入文件: {csv_file}")
  19. with open(csv_file, 'r', encoding='utf-8') as f:
  20. reader = csv.reader(f)
  21. # 读取标题行
  22. try:
  23. header = next(reader)
  24. num_columns = len(header)
  25. print(f"[{time.strftime('%H:%M:%S')}] 检测到 {num_columns} 列 | 标题: {', '.join(header)}")
  26. except StopIteration:
  27. print("❌ 错误:CSV文件为空或格式不正确")
  28. exit()
  29. # 准备SQL语句
  30. placeholders = ', '.join(['?'] * num_columns)
  31. sql = f"INSERT INTO flowlist VALUES ({placeholders})"
  32. print(f"[{time.strftime('%H:%M:%S')}] 生成SQL语句: {sql}\n")
  33. # 插入数据
  34. total_rows = 0
  35. for i, row in enumerate(reader, 1):
  36. # 关键修改:将空白值转换为0
  37. processed_row = [
  38. 0 if str(cell).strip() == '' else cell # 空白转0,保留非空值
  39. for cell in row
  40. ]
  41. cursor.execute(sql, processed_row)
  42. total_rows += 1
  43. # 每100行提示进度
  44. if i % 10 == 0:
  45. print(f"[{time.strftime('%H:%M:%S')}] 已插入 {i} 行...", end='\r')
  46. # 提交事务
  47. conn.commit()
  48. print(f"\n[{time.strftime('%H:%M:%S')}] ✅ 数据插入完成,共 {total_rows} 行")
  49. except FileNotFoundError:
  50. print(f"❌ 错误:CSV文件不存在,当前搜索路径: {os.path.abspath(csv_file)}")
  51. except sqlite3.Error as e:
  52. print(f"❌ 数据库错误: {str(e)}")
  53. conn.rollback()
  54. except Exception as e:
  55. print(f"❌ 发生异常: {str(e)}")
  56. finally:
  57. if 'conn' in locals():
  58. conn.close()
  59. print(f"[{time.strftime('%H:%M:%S')}] 数据库连接已关闭")
  60. # 计算总耗时
  61. end_time = time.time()
  62. print(f"\n总耗时: {end_time - start_time:.2f} 秒")