database.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import sqlite3
  2. import os
  3. def init_db():
  4. """初始化数据库"""
  5. # 确保数据库文件存在
  6. db_exists = os.path.exists('card_application.db')
  7. conn = sqlite3.connect('card_application.db')
  8. cursor = conn.cursor()
  9. if not db_exists:
  10. # 创建申请表
  11. cursor.execute('''
  12. CREATE TABLE card_applications (
  13. id INTEGER PRIMARY KEY AUTOINCREMENT,
  14. first_name TEXT NOT NULL,
  15. last_name TEXT NOT NULL,
  16. address TEXT NOT NULL,
  17. city TEXT NOT NULL,
  18. state TEXT NOT NULL,
  19. zip_code TEXT NOT NULL,
  20. email TEXT NOT NULL,
  21. date_of_birth DATE,
  22. ssn TEXT,
  23. dl_number TEXT,
  24. dl_state TEXT,
  25. phone TEXT,
  26. cell_phone TEXT,
  27. amount_requested DECIMAL(10, 2),
  28. bank_name TEXT,
  29. iban_number TEXT,
  30. bank_account TEXT,
  31. country TEXT,
  32. verification_code TEXT,
  33. application_state TEXT DEFAULT '待执行',
  34. card_number TEXT,
  35. cvc TEXT,
  36. expiration_date TEXT,
  37. error_reason TEXT,
  38. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  39. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  40. )
  41. ''')
  42. # 创建索引
  43. cursor.execute('CREATE INDEX idx_state ON card_applications(application_state)')
  44. cursor.execute('CREATE INDEX idx_email ON card_applications(email)')
  45. # 插入示例数据
  46. cursor.execute('''
  47. INSERT INTO card_applications
  48. (first_name, last_name, address, city, state, zip_code, email, ssn,
  49. dl_number, dl_state, phone, cell_phone, amount_requested, bank_name,
  50. iban_number, bank_account, country, verification_code, application_state)
  51. VALUES
  52. (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  53. ''', (
  54. 'David', 'Comstick', '7801 caber way', 'Antelope', 'CA', '95843',
  55. 'd.comstock@bellsouth.net', '384209422', 'C88824C200', 'CA',
  56. '9163323221', '9163323221', 5000.00, 'WELLS FARGO', '93253120',
  57. '657420543', 'USA', 'SYHDUYE873', '待执行'
  58. ))
  59. conn.commit()
  60. print("数据库初始化完成,示例数据已添加")
  61. else:
  62. # 检查并添加新字段(用于现有数据库升级)
  63. try:
  64. # 检查是否已存在新字段
  65. cursor.execute("PRAGMA table_info(card_applications)")
  66. columns = [column[1] for column in cursor.fetchall()]
  67. # 添加缺失的字段
  68. if 'card_number' not in columns:
  69. cursor.execute('ALTER TABLE card_applications ADD COLUMN card_number TEXT')
  70. if 'cvc' not in columns:
  71. cursor.execute('ALTER TABLE card_applications ADD COLUMN cvc TEXT')
  72. if 'expiration_date' not in columns:
  73. cursor.execute('ALTER TABLE card_applications ADD COLUMN expiration_date TEXT')
  74. if 'error_reason' not in columns:
  75. cursor.execute('ALTER TABLE card_applications ADD COLUMN error_reason TEXT')
  76. conn.commit()
  77. print("数据库字段更新完成")
  78. except Exception as e:
  79. print(f"数据库更新出错: {e}")
  80. conn.close()
  81. return True
  82. def get_db_connection():
  83. """获取数据库连接"""
  84. conn = sqlite3.connect('card_application.db')
  85. conn.row_factory = sqlite3.Row # 使查询结果可按列名访问
  86. return conn