video_monitor/app/utils/Database.py
2026-08-30 22:23:12 +08:00

37 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import threading
from django.db import connection
g_dbLock = threading.Lock()# 用于操作数据库的全局锁20240930新增由于sqlite不支持锁因此在程序中做锁控制
class Database(object):
def __init__(self, logger):
self.logger = logger
def select(self, sql, params=None):
data = []
with g_dbLock:
cursor = connection.cursor()
cursor.execute(sql, params or None)
try:
rawData = cursor.fetchall()
col_names = [desc[0] for desc in cursor.description]
for row in rawData:
d = {}
for index, value in enumerate(row):
d[col_names[index]] = value
data.append(d)
except Exception as e:
self.logger.error("Database.select() error:%s,sql:%s" % (str(e),sql))
return data
def execute(self, sql, params=None):
ret = False
with g_dbLock:
try:
cursor = connection.cursor()
cursor.execute(sql, params or None)
ret = True
except Exception as e:
self.logger.error("Database.execute() error:%s,sql:%s" % (str(e), sql))
return ret