python
import pyodbc
import traceback
import config_json
import logging
class Server():
def __init__(self):
SERVER = '10.0.100.100'
USERNAME = 'ma'
PASSWORD = '123456'
DATABASE = 'caiji'
# self.connectionString = f'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={SERVER};DATABASE={DATABASE};ENCRYPT=yes;TrustServerCertificate=yes;UID={USERNAME};PWD={PASSWORD}'
self.connectionString = 'DRIVER=ODBC Driver 18 for SQL Server;SERVER=%s;DATABASE=%s;ENCRYPT=yes;TrustServerCertificate=yes;UID=%s;PWD=%s' %(SERVER,DATABASE,USERNAME,PASSWORD)
def updateDb(self):
conn = pyodbc.connect(self.connectionString)
cursor = self.conn.cursor()
cmds=codecs.open("caiji.sql","r",'utf-8').read().split(";")
for cmd in cmds:
print(cmd)
cursor.execute(cmd)
conn.commit()
cursor.close()
conn.close()
先使用的pymssql,没成功.