22 lines
1.1 KiB
Python
22 lines
1.1 KiB
Python
![]() |
def SavePresetValuetoSQLSERVER(table_name,mydict):
|
||
|
import pymssql #在python中编辑SQL SERVER数据库
|
||
|
import Self_defined_functions #引入自定义函数文件
|
||
|
#mydict = {'高压侧定值':167,'低压侧定制':666,'有毛病定值':981}
|
||
|
conn = Self_defined_functions.connectMainDatabase() #连接至ISMS_BASE数据库
|
||
|
cursor = conn.cursor() #获取pymssql提供的SQL Server编辑游标
|
||
|
#SQL_Truncate_table = "TRUNCATE TABLE im_PresetValue_AT"
|
||
|
#cursor.execute(SQL_Truncate_table)
|
||
|
# 需要插入数据的表名和字段名
|
||
|
#table_name = "im_PresetValue_AT"
|
||
|
sql_truncate = f"TRUNCATE TABLE {table_name}"
|
||
|
sql_insert = f"INSERT INTO {table_name} (PresetValueName, Value) VALUES (%s, %d)"
|
||
|
cursor.execute(sql_truncate)
|
||
|
conn.commit()
|
||
|
# 遍历字典并插入每个定值
|
||
|
for preset_name, preset_value in mydict.items():
|
||
|
cursor.execute(sql_insert, (preset_name, preset_value))
|
||
|
|
||
|
# 提交更改并关闭连接
|
||
|
conn.commit() # 提交事务,确保更改生效
|
||
|
cursor.close() # 关闭游标
|
||
|
conn.close() # 关闭数据库连接
|