fix: 数据库insert方法使用INSERT OR REPLACE更新已存在记录
This commit is contained in:
@@ -108,31 +108,20 @@ class DailyLogsDatabase:
|
|||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def insert(self, log: Dict) -> bool:
|
def insert(self, log: Dict) -> bool:
|
||||||
"""插入记录(存在则跳过,不存在则插入)"""
|
"""插入记录(存在则替换,不存在则插入)"""
|
||||||
try:
|
try:
|
||||||
cursor = self.conn.cursor()
|
cursor = self.conn.cursor()
|
||||||
# 检查是否已存在
|
# 使用 INSERT OR REPLACE 来更新已存在的记录
|
||||||
cursor.execute('''
|
cursor.execute('''
|
||||||
SELECT id FROM daily_handover_logs
|
INSERT OR REPLACE INTO daily_handover_logs
|
||||||
WHERE date = ? AND shift = ? AND ship_name = ?
|
(date, shift, ship_name, teu, efficiency, vehicles, created_at)
|
||||||
''', (log['date'], log['shift'], log['ship_name']))
|
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
||||||
existing = cursor.fetchone()
|
''', (
|
||||||
|
log['date'], log['shift'], log['ship_name'],
|
||||||
if existing:
|
log.get('teu'), log.get('efficiency'), log.get('vehicles')
|
||||||
# 记录已存在,跳过
|
))
|
||||||
return False
|
self.conn.commit()
|
||||||
else:
|
return True
|
||||||
# 插入新记录
|
|
||||||
cursor.execute('''
|
|
||||||
INSERT INTO daily_handover_logs
|
|
||||||
(date, shift, ship_name, teu, efficiency, vehicles, created_at)
|
|
||||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
||||||
''', (
|
|
||||||
log['date'], log['shift'], log['ship_name'],
|
|
||||||
log.get('teu'), log.get('efficiency'), log.get('vehicles')
|
|
||||||
))
|
|
||||||
self.conn.commit()
|
|
||||||
return True
|
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
print(f"数据库错误: {e}")
|
print(f"数据库错误: {e}")
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user