首页 技术 正文
技术 2022年11月20日
0 收藏 920 点赞 3,280 浏览 2271 个字

import pymssql

class MSSQL:
    “””
    对pymssql的简单封装
    pymssql库,该库到这里下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql
    使用该库时,需要在Sql Server Configuration Manager里面将TCP/IP协议开启

用法:

“””

def __init__(self,host,user,pwd,db):
        self.host = host
        self.user = user
        self.pwd = pwd
        self.db = db

def __GetConnect(self):
        “””
        得到连接信息
        返回: conn.cursor()
        “””
        if not self.db:
            raise(NameError,”没有设置数据库信息”)
        self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset=”utf8″)
        cur = self.conn.cursor()
        if not cur:
            raise(NameError,”连接数据库失败”)
        else:
            return cur

def ExecQuery(self,sql):
        “””
        执行查询语句
        返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段

调用示例:
                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.close()
        return resList

def ExecNonQuery(self,sql):
        “””
        执行非查询语句

调用示例:
            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 main():
## ms = MSSQL(host=”localhost”,user=”sa”,pwd=”123456″,db=”PythonWeiboStatistics”)
## #返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段
## ms.ExecNonQuery(“insert into WeiBoUser values(‘2′,’3’)”)

ms = MSSQL(host=”localhost”,user=”sa”,pwd=”123456″,db=”PythonWeiboStatistics”)
    resList = ms.ExecQuery(“SELECT id,weibocontent FROM WeiBo”)
    for (id,weibocontent) in resList:
        print str(weibocontent).decode(“utf8”)

if __name__ == ‘__main__’:
    main()  注意事项:    使用pymssql进行中文操作时候可能会出现中文乱码,我解决的方案是:

    • 文件头加上 #coding=utf8
    • sql语句中有中文的时候进行encode   insertSql = “insert into WeiBo([UserId],[WeiBoContent],[PublishDate]) values(1,’测试’,’2012/2/1′)”.encode(“utf8”)
    •  连接的时候加入charset设置信息    pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset=”utf8″)
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918