完整功能
This commit is contained in:
5
database/__init__.py
Normal file
5
database/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
"""数据库包"""
|
||||
from .access_db import AccessDB
|
||||
from .sql_server import SQLServerDB
|
||||
|
||||
__all__ = ['AccessDB', 'SQLServerDB']
|
||||
BIN
database/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
database/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
database/__pycache__/access_db.cpython-39.pyc
Normal file
BIN
database/__pycache__/access_db.cpython-39.pyc
Normal file
Binary file not shown.
BIN
database/__pycache__/sql_server.cpython-39.pyc
Normal file
BIN
database/__pycache__/sql_server.cpython-39.pyc
Normal file
Binary file not shown.
BIN
database/__pycache__/sql_server_connection.cpython-39.pyc
Normal file
BIN
database/__pycache__/sql_server_connection.cpython-39.pyc
Normal file
Binary file not shown.
61
database/access_db.py
Normal file
61
database/access_db.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""Access数据库操作模块"""
|
||||
import pyodbc
|
||||
|
||||
|
||||
class AccessDB:
|
||||
def __init__(self, db_path, password):
|
||||
self.db_path = db_path
|
||||
self.password = password
|
||||
self.connection = None
|
||||
|
||||
def connect(self):
|
||||
"""连接Access数据库"""
|
||||
conn_str = (
|
||||
r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
|
||||
f'DBQ={self.db_path};'
|
||||
f'PWD={self.password};'
|
||||
)
|
||||
self.connection = pyodbc.connect(conn_str)
|
||||
return self.connection
|
||||
|
||||
def get_max_mark(self):
|
||||
"""获取Access数据库中最大的Mark值"""
|
||||
if not self.connection:
|
||||
self.connect()
|
||||
|
||||
cursor = self.connection.cursor()
|
||||
cursor.execute("SELECT MAX(Mark) FROM Produce")
|
||||
max_mark = cursor.fetchone()[0]
|
||||
|
||||
if max_mark is None:
|
||||
max_mark = 0
|
||||
|
||||
return max_mark
|
||||
|
||||
def query_task_status(self, mis_ids):
|
||||
"""查询任务状态"""
|
||||
if not self.connection:
|
||||
self.connect()
|
||||
|
||||
if not mis_ids:
|
||||
return {}
|
||||
|
||||
cursor = self.connection.cursor()
|
||||
placeholders = ','.join('?' * len(mis_ids))
|
||||
query = f"SELECT MISID, Flag FROM Produce WHERE MISID IN ({placeholders})"
|
||||
cursor.execute(query, mis_ids)
|
||||
results = cursor.fetchall()
|
||||
|
||||
current_tasks = {}
|
||||
for row in results:
|
||||
mark = row[0]
|
||||
flag = row[1] if row[1] is not None else ""
|
||||
current_tasks[mark] = flag
|
||||
|
||||
return current_tasks
|
||||
|
||||
def close(self):
|
||||
"""关闭数据库连接"""
|
||||
if self.connection:
|
||||
self.connection.close()
|
||||
self.connection = None
|
||||
61
database/sql_server.py
Normal file
61
database/sql_server.py
Normal file
@ -0,0 +1,61 @@
|
||||
"""SQL Server数据库操作模块"""
|
||||
import pyodbc
|
||||
from config.settings import SQL_SERVER_CONFIG
|
||||
|
||||
|
||||
class SQLServerDB:
|
||||
def __init__(self):
|
||||
self.connection = None
|
||||
self.config = SQL_SERVER_CONFIG
|
||||
|
||||
def connect(self):
|
||||
"""连接SQL Server数据库"""
|
||||
connection_string = (
|
||||
"DRIVER={SQL Server};"
|
||||
f"SERVER={self.config['server']};"
|
||||
f"DATABASE={self.config['database']};"
|
||||
f"UID={self.config['username']};"
|
||||
f"PWD={self.config['password']};"
|
||||
)
|
||||
self.connection = pyodbc.connect(connection_string)
|
||||
return self.connection
|
||||
|
||||
def insert_produce_data(self, insert_data):
|
||||
"""插入数据到Produce表"""
|
||||
if not self.connection:
|
||||
self.connect()
|
||||
|
||||
cursor = self.connection.cursor()
|
||||
columns = ", ".join(insert_data.keys())
|
||||
placeholders = ", ".join(["?" for _ in insert_data.values()])
|
||||
sql = f"INSERT INTO Produce ({columns}) VALUES ({placeholders})"
|
||||
|
||||
cursor.execute(sql, list(insert_data.values()))
|
||||
self.connection.commit()
|
||||
|
||||
return cursor.rowcount
|
||||
|
||||
def check_existing_tasks(self, erp_ids):
|
||||
"""检查SQL Server中任务是否还存在"""
|
||||
if not self.connection:
|
||||
self.connect()
|
||||
|
||||
if not erp_ids:
|
||||
return set()
|
||||
|
||||
erp_ids_str = [str(erp_id) for erp_id in erp_ids]
|
||||
placeholders = ','.join('?' * len(erp_ids_str))
|
||||
check_query = f"SELECT ErpID FROM Produce WHERE ErpID IN ({placeholders})"
|
||||
|
||||
cursor = self.connection.cursor()
|
||||
cursor.execute(check_query, erp_ids_str)
|
||||
results = cursor.fetchall()
|
||||
|
||||
existing_tasks = {str(row[0]) for row in results}
|
||||
return existing_tasks
|
||||
|
||||
def close(self):
|
||||
"""关闭数据库连接"""
|
||||
if self.connection:
|
||||
self.connection.close()
|
||||
self.connection = None
|
||||
63
database/sql_server_connection.py
Normal file
63
database/sql_server_connection.py
Normal file
@ -0,0 +1,63 @@
|
||||
# database/sql_server_connection.py
|
||||
"""SQL Server数据库连接模块"""
|
||||
import pyodbc
|
||||
from config.settings import SQL_SERVER_CONFIG
|
||||
|
||||
class SQLServerConnection:
|
||||
def __init__(self):
|
||||
self.config = SQL_SERVER_CONFIG
|
||||
|
||||
def connect(self):
|
||||
"""建立数据库连接"""
|
||||
connection_string = (
|
||||
f"DRIVER={self.config['driver']};"
|
||||
f"SERVER={self.config['server']};"
|
||||
f"DATABASE={self.config['database']};"
|
||||
f"UID={self.config['username']};"
|
||||
f"PWD={self.config['password']};"
|
||||
)
|
||||
return pyodbc.connect(connection_string)
|
||||
|
||||
def get_mix_weight_info(self):
|
||||
"""
|
||||
获取所有配比号及其对应每方重量的信息
|
||||
重量计算方式:U1到U17的和,NULL值不计入
|
||||
"""
|
||||
connection = self.connect()
|
||||
cursor = connection.cursor()
|
||||
|
||||
try:
|
||||
# 查询所有配比信息
|
||||
query = """
|
||||
SELECT
|
||||
Code,
|
||||
U1, U2, U3, U4, U5, U6, U7, U8, U9, U10, U11, U12, U13, U14, U15, U16, U17
|
||||
FROM MixTable
|
||||
WHERE Code IS NOT NULL
|
||||
ORDER BY Code
|
||||
"""
|
||||
|
||||
cursor.execute(query)
|
||||
results = cursor.fetchall()
|
||||
|
||||
mix_info = []
|
||||
for row in results:
|
||||
code = row[0]
|
||||
weights = [row[i] for i in range(1, 18)] # U1到U17
|
||||
|
||||
# 计算总重量(忽略NULL值)
|
||||
total_weight = sum(weight for weight in weights if weight is not None and weight != 'NULL')
|
||||
|
||||
mix_info.append({
|
||||
"Code": code,
|
||||
"TotalWeight": round(total_weight, 2) # 保留两位小数
|
||||
})
|
||||
|
||||
return mix_info
|
||||
|
||||
except Exception as e:
|
||||
print(f"查询配比信息时出错: {e}")
|
||||
return []
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
Reference in New Issue
Block a user