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,如需转载请自行联系原作者

相关文章
|
7月前
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
314 13
|
7月前
|
SQL 关系型数据库 MySQL
MySQL数据库连接过多(Too many connections)错误处理策略
综上所述,“Too many connections”错误处理策略涉及从具体参数配置到代码层面再到系统与架构设计全方位考量与改进。每项措施都需根据具体环境进行定制化调整,并且在执行任何变更前建议先行测试评估可能带来影响。
1594 11
|
7月前
|
Unix Linux Shell
指定端口-SSH连接的目标(告别 22 端口暴力破解)
本文介绍了 SSH 命令 `ssh -p 44907 root@IP` 的含义与使用方法,包括命令结构拆解、完整示例及执行过程详解,帮助用户安全地远程登录服务器。
1073 0
|
9月前
|
SQL XML Java
配置Spring框架以连接SQL Server数据库
最后,需要集成Spring配置到应用中,这通常在 `main`方法或者Spring Boot的应用配置类中通过加载XML配置或使用注解来实现。
679 0
|
12月前
|
Oracle 安全 关系型数据库
【Oracle】使用Navicat Premium连接Oracle数据库两种方法
以上就是两种使用Navicat Premium连接Oracle数据库的方法介绍,希望对你有所帮助!
2341 28
|
12月前
|
SQL 数据库连接 数据库
在C++的QT框架中实现SQLite数据库的连接与操作
以上就是在C++的QT框架中实现SQLite数据库的连接与操作的基本步骤。这些步骤包括创建数据库连接、执行SQL命令、处理查询结果和关闭数据库连接。在实际使用中,你可能需要根据具体的需求来修改这些代码。
744 14
|
关系型数据库 数据库 Python
Python连接DB2数据库
Python连接DB2数据库
419 0
|
SQL 关系型数据库 MySQL
用 Python 连接数据库并进行查询。
【2月更文挑战第12天】【2月更文挑战第32篇】用 Python 连接数据库并进行查询。
276 0

推荐镜像

更多