Python 插入、删除、更新、变量、批量处理到 Sql_server
最编程
2024-07-15 13:00:52
...
import pymssql
connect = pymssql.connect('127.0.0.1', 'sa', '********', 'test')
cursor = connect.cursor()
sql = "select * from A8SD_GLDEPARTMENT where year = 2020 and ACCOUNTID = 001 and (oadepid = 1703065553569008174 OR oadepid = 3932086535711443527)"
#查询想要的数据转换成变量
cursor.execute(sql)
row = cursor.fetchall()
#把查询的结果转换成list列,
for i in row:
#进行循环处理
#print(i[1], i[4], i[5])
#循环打印当前列的 1 、4、 5的值
sql1 = "update A8SD_GLDEPARTMENT set UFDEPID = '%s', UFDEPNAME = '%s' where ACCOUNTID <> 001 and oadepid = '%s';commit "% (i[4], i[5], i[1])
#对自己的数据表进行数据更新处理
try:
cursor.execute(sql1)
except:
connect.rollback()
cursor.close()
connect.close()