开发者社区> 科技小先锋> 正文

python 使用pymssql 连接MSSQL数据库(带端口)

简介:
+关注继续查看

知识点:如果连接数据库不使用默认端口,需要在连接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)



本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/752012,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Python编程:MySQLdb模块对数据库的基本增删改查操作
Python编程:MySQLdb模块对数据库的基本增删改查操作
37 0
Python编程:PyThink数据库交互模块提高爬虫编写速度
Python编程:PyThink数据库交互模块提高爬虫编写速度
27 0
开心档-软件开发入门之Python 操作 MySQL 数据库
本文主要讲解Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口。
47 0
基于创建的mogodb数据库,用python分析股票的跳空缺口
基于创建的mogodb数据库,用python分析股票的跳空缺口
26 0
软件测试|Python基础之数据库
软件测试|Python基础之数据库
20 0
【python】链接sql server数据库,并实现简单的增删改查(1)
Python编程语言越来越受到大家的喜爱,本篇文章就从链接微软数据库进行增删改查操作的讲解
34 0
【python】连接sql server数据库,并实现简单的增删改查(1)
Python编程语言越来越受到大家的喜爱,本篇文章就从链接微软数据库进行增删改查操作的讲解
86 0
Python连接Oracle数据库完美教程
Python连接Oracle数据库完美教程
71 0
Python 运用GeoIP2离线数据库定位
GeoIP2 是一个强大的离线数据库,该数据库内定义并记录了目前所有主机的IP地址和所在位置,通过传入某个IP地址,即可精确的定位到主机的位置,再结合谷歌地图可完美的画出坐标。
78 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
给运维工程师的Python实战课
立即下载
Python 脚本速查手册
立即下载
ACE 区域技术发展峰会:Flink Python Table API入门及实践
立即下载