小标
2018-10-19
来源 :
阅读 1588
评论 0
摘要:本文主要向大家介绍了SQLServer数据库之python访问sql server安装、配置、代码示例,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。
本文主要向大家介绍了SQLServer数据库之python访问sql server安装、配置、代码示例,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。
freeTDS是能够用Linux和Unix连接MS SQLServer和Sybase数据库,TDS的意思是"表列数据流"安装gcc组件:yum install -y gcc否则configure的时候报错:configure: error: no acceptable C compiler found in $PATHLinux下安装freetds-dev:download source://mirrors.ibiblio.org/freetds/stable/freetds-stable.tgz//www.freetds.org/在任意目录解压后:./configure --prefix=/usr/local/freetds --with-tdsver=8.0 makesudo make install配置文件:/usr/local/freetds/etc/freetds.conf添加:[mssql2005] host = x.x.x.x port = 1433 tds version = 8.0 client charset = UTF-8测试:# /usr/local/freetds/bin/tsql -S mssql2005 -H x.x.x.x -p 1433 -U MyUser -P MyPassword1> select * from MyDB..MyTable2> go(正常可以得到查询结果)下载python setuptools-githttps://pypi.python.org/pypi/setuptools-git/解压到任意目录,进入目录运行:python setup.py install下载安装pymssql:https://pypi.python.org/pypi/pymssql/2.1.1#downloadspymssql-2.1.1.tar.gz解压到任意目录,进入目录运行:python setup.py install增加lib路径:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/freetds/lib/即可在python中import pymssql增加bin路径:vi /root/.bashrc 添加内容如下: export FREETDS=/usr/local/freetds export $PATH="$PATH:$FREETDS/bin"使其立即生效:source /root/.bashrc (end)代码示例:#coding=utf-8
#!/usr/bin/env python
import pymssql
import ConfigParser
class MSSQL:
def __init__(self):
cf = ConfigParser.ConfigParser()
cf.read("mssql.conf")
self.host = cf.get("DB","host")
self.user = cf.get("DB","user")
self.pwd = cf.get("DB","pwd")
self.db = cf.get("DB","db")
def __GetConnect(self):
"""
get connetion info
response: conn.cursor()
"""
#if not self.db:
# raise(NameError,"no db conf file found")
self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,timeout=5,login_timeout=2,charset="utf8")
cur = self.conn.cursor()
if not cur:
raise(NameError,"fail connecting to DB")
else:
return cur
##verify DB connection
def VerifyConnection(self):
try:
if self.host==‘‘:
return False
conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,timeout=1,login_timeout=1,charset="utf8")
return True
except:
return False
def ExecQuery(self,sql):
"""
execute query
get a list including tuple, elements of list are row of record, elements of tuple is fields
demo
ms = MSSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser")
for (id,NickName) in resList:
print str(id),NickName
"""
cur = self.__GetConnect()
cur.execute(sql)
resList = cur.fetchall()
#resList = cur.description
#close connection after querying
self.conn.close()
return resList
def ExecNonQuery(self,sql):
"""
execute no query
demo
cur = self.__GetConnect()
cur.execute(sql)
self.conn.commit()
self.conn.close()
"""
cur = self.__GetConnect()
cur.execute(sql)
self.conn.commit()
self.conn.close()
def ExecStoreProduce(self,sql):
"""
execute query
get a list including tuple, elements of list are row of record, elements of tuple is fields
demo:
ms = MSSQL(host="localhost",user="sa",pwd="123456",db="PythonWeiboStatistics")
resList = ms.ExecQuery("SELECT id,NickName FROM WeiBoUser")
for (id,NickName) in resList:
print str(id),NickName
"""
cur = self.__GetConnect()
cur.execute(sql)
resList = cur.fetchall()
self.conn.commit()
#close connection after querying
self.conn.close()
return resList
def main():
sqlquery="select * from MyDB..MyTable"
sqlconn=MSSQL()
res=sqlconn.ExecQuery(sqlquery)
for data in res:
print data
if __name__==‘__main__‘:
main()
配置文件mssql.conf:[DB]
host=x.x.x.x
db=MyDB
user=MyUser
pwd=MyPassword
本文由职坐标整理并发布,希望对同学们学习SQL Server有所帮助,更多内容请关注职坐标数据库SQL Server数据库频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号