python 链接云端数据库/远程数据库 可以使用原始Odbc
class MySqlOdbc:
def __init__(self):
self.sqlhead = None # 当前数据链接句柄
self.mycursor = None # 当前游标
"""
关闭数据库
"""
def close(self):
if self.mycursor:
self.mycursor.close()
"""
链接云端远程数据库 ,isautocommit:自动提交,可以省去手动self.mycursor.commit()过程; 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.sqlhead = pyodbc.connect(p_strinfo, autocommit=isautocommit, ansi=isansi, timeout=ntimeout)
self.mycursor = self.sqlhead.cursor()
return False
except Exception as errinfo:
print(errinfo)
return True
"""
查询远程数据库
sqlcmd = "select * from tables where id = '123'"
"""
def query(self, sqlcmd):
if self.mycursor:
self.mycursor.execute(sqlcmd)
rows = self.mycursor.fetchall() # 取得所有结果
for irow in rows: # 取得1行结果
for ir in irow: # 取得1行结果tuple
print(ir)
def executecmd(self,sqlcmd):
# insert into tbSnInfo(SN,Type,IsValid) values('%s','%s','%s')
# UPDATE tbSnInfo SET IsValid='False' WHERE SN = '%s'
return self.mycursor.execute(sqlcmd)
本文来自博客园,作者:{archer},转载请注明原文链接:https://www.cnblogs.com/archer-mowei/p/15823291.html