1. 程式人生 > >2019/1/15 批量刪除數據庫相關數據

2019/1/15 批量刪除數據庫相關數據

eric 目的 his 測試 check sde exe lose lang

目的:刪除數據庫各個系統his庫裏的測試soe與故障數據;
一、sql執行錯誤:
執行sql:SELECT FROM SOE WHERE sDevCode = 012400000001
報錯:
[SQL Server]將 varchar 轉換為數據類型 numeric 時出現算術溢出錯誤。
原因:查看表設計,sDevCode列的類型為varchar(字符串),
修改:SELECT
FROM SOE WHERE sDevCode = ‘012400000001‘ 。
二、腳本;

-- coding:UTF-8 --

import os
import pymssql
import pprint
sql_name = [
qqqqq
]
print(‘****

程序正在啟動****‘)
os.environ[‘NLS_LANG‘] = ‘SIMPLIFIED CHINESE_CHINA.UTF8‘
os.environ[‘NLS_LANG‘] = ‘AMERICAN_AMERICA.AL32UTF8‘
for SJK in sql_name:
IP_HIS = SJK[0] # 獲取數據庫IP
us_HIS = SJK[1]
PW_HIS= SJK[2]
HIS = SJK[3]
print(HIS)
conn = pymssql.connect(host=IP_HIS, user=us_HIS, password=PW_HIS, database=HIS)
cur1 = conn.cursor()
check_sql1 = "DELETE FROM SOE WHERE sDevCode = ‘012400000001‘"
cur1.execute(check_sql1)
cur2 = conn.cursor()
check_sql2 = "DELETE FROM Fault_Record WHERE LOWER(sLineName) LIKE ‘%cs%‘ or sLineName LIKE ‘%測試%‘ or sLineName LIKE ‘%模擬%‘"
cur2.execute(check_sql2)
conn.close()

解析:主要在sql上,
DELETE FROM Fault_Record WHERE LOWER(sLineName) LIKE ‘%cs%‘ or sLineName LIKE ‘%測試%‘ or sLineName LIKE ‘%模擬%

其中,模糊查詢用like ,忽略大小寫用lower或者upper都可以。

2019/1/15 批量刪除數據庫相關數據