class MySqlOdbc:
def __init__(self):
self.islink_success = False # 是否链接成功
self.sqlhead = None # 当前数据链接句柄
self.mycursor = None # 当前游标
"""
链接云端远程数据库 ,isautocommit:自动提交, PORT:端口一般随意,除非有限制
"""
#p_strinfo = 'DRIVER={SQL Server};SERVER=222.222.166.222;PORT=1433;DATABASE=sqldbname;UID=username;PWD=123456'
def linksql(self,p_strinfo, isautocommit=True, isansi=False, ntimeout=0):
try:
import pyodbc
self.islink_success = False
self.sqlhead = pyodbc.connect(p_strinfo, autocommit=isautocommit, ansi=isansi, timeout=ntimeout)
self.islink_success = True
self.mycursor = self.sqlhead.cursor()
except Exception as errinfo:
print(errinfo)
return self.islink_success
"""
查询远程数据库
sqlcmd = "select * from tables where id = '123'"
"""
def query(self, sqlcmd):
self.mycursor.execute(sqlcmd)
rows = self.mycursor.fetchall() # 取得所有结果
for irow in rows: # 取得1行结果
for ir in irow: # 取得1行结果tuple
print(ir)