前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 使用pymssql 连接M

python 使用pymssql 连接M

作者头像
py3study
发布2020-01-09 17:03:19
5500
发布2020-01-09 17:03:19
举报
文章被收录于专栏:python3python3

知识点:如果连接数据库不使用默认端口,需要在连接host地址上加上端口

如cacelbert01.mysql.alibabalabs.com:3306

#coding=gbk

############################################################## # Copyright (C), 2009-2010, aliyun # FileName: dbunit.py # Author: elbert.chenh # Version: 0.1 # History: # <Author/Maintainer> <Date> <Modification> # elbert.chenh 10/07/11 Create this file ############################################################# import sys import ConfigParser import datetime,time import binascii import os import types import os import pdb import pymssql class DBUnit: def __init__(self,user=None,passwd=None,host=None,database=None): try: self.connection = pymssql.connect(host=host, user = user, password =passwd, database=database) self.cursor= self.connection.cursor() except: print "Could not connect to DB server." exit(0) def __del__(self): self.cursor.close() self.connection.close() def read(self,Sql,param=None): '''Exec select sql , return type is Tuple,use len fun return select row num use param like this: Sql=select * from table where param=%s and param1=%s param=(value1,valuei2) ''' try: cursor = self.connection.cursor() if param==None: cursor.execute(Sql) rs = cursor.fetchall() cursor.close() else: cursor.execute(Sql,param) rs = cursor.fetchall() cursor.close() except Exception,e: print e rs = () return rs def write(self,sql,param,iscommit=True): try: cursor = self.connection.cursor() print sql n = cursor.executemany(sql,param) if iscommit : self.connection.commit() return n except Exception,e: print e self.connection.rollback() return -1 def writeOneRecord(self,sql): try: cursor = self.connection.cursor() n = cursor.execute(sql) self.connection.commit() return int(cursor.lastrowid) except: self.connection.rollback() return -1 if __name__ == '__main__': a = time.time() db = DBUnit('accelbert08','a1234561','cacelbert01.mysql.alibabalabs.com:3306','elbert08') //不使用默认端口 rs = db.read("select count(*) from t_file") print rs #db.delete(dictinu)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档