python连接sql server(转载) 【Python连接SQL Server数据库】-pymssql使用基础 python操作sqlserver数据库

pymssql不等于pymysql,pymssql用于sql server,pymysql用于mysql

【Python连接SQL Server数据库】-pymssql使用基础

Python操作SQLServer示例—注意事项和说明 :

1. 本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文)。
2. 需要注意的是:读取数据的时候需要decode('utf-8'),写数据的时候需要encode('utf-8'),这样就可以避免烦人的中文乱码或报错问题。
3. Python操作SQLServer需要使用pymssql模块,使用pip install pymssql安装即可。

  
  
  • 1
  • 2
  • 3

2. python 链接sqlserver数据库的源码:

此外代码中使用的封装MSSQL类是从网上搜索到的,代码可以封装成一个工具类,直接用即可。

  
  
  • 1
# -*- coding:utf-8 -*-
import pymssql
class MSSQL:
    def __init__(self,host,user,pwd,db):
        self.host = host
        self.user = user
        self.pwd = pwd
        self.db = db

    def __GetConnect(self):
        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):
        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()

ms = MSSQL(host="192.168.1.1",user="sa",pwd="sa",db="testdb")
reslist = ms.ExecQuery("select * from webuser")
for i in reslist:
    print i

newsql="update webuser set name='%s' where id=1"%u'测试'
print newsql
ms.ExecNonQuery(newsql.encode('utf-8'))
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

3. 博客内容来源:

https://www.cnblogs.com/lrzy/p/4346781.html
https://www.cnblogs.com/baiyangcao/p/pymssql_basic.html

python操作sqlserver数据库

用Python操作sqlserver数据库时,要先安装用于Python链接sqlserver数据库的接口---pymssql

下载地址:https://pypi.org/project/pymssql/#files

我选择的是:

下载完成之后,即可开始进行安装。

我下载完成之后,习惯放在了D盘(C盘不够用……手动捂脸)。

用命令提示符:cmd进行安装

点击开始,输入cmd回车

然后输入安装指令:pip install pymssql-2.1.4-cp37-cp37m-win_amd64.whl(注意:这里是自己下载版本的名称)

出错了。。。。。duang,想了一下,我的whl文件不在C盘下,

利用命令改变路径,重新安装,OK.

安装完成之后,在Python的项目中输入:import pymssql 

没有出错则表示安装成功。

现在来测试一下是否可以链接数据库进行操作。

成功查询出结果。

附上简单的查询代码


  
  
  1. #coding=utf-8
  2. # sqlserver的连接
  3. import pymssql
  4. # 打开数据库连接 这里的host='.'也可用本机ip或ip+端口号(sqlserver默认端口号:1433)
  5. conn = pymssql.connect(host=".",user= "sa",password= "1234", database="onlinemall", charset='utf8' )
  6. # 使用cursor()方法获取操作游标
  7. cursor = conn.cursor()
  8. # SQL 查询语句
  9. sql = "SELECT * FROM region"
  10. try:
  11. # 执行SQL语句
  12. cursor.execute(sql)
  13. # 获取所有记录列表
  14. results = cursor.fetchall()
  15. print(results)
  16. except:
  17. print(results)
  18. # 关闭数据库连接
  19. conn.close()

到此Python链接sqlserver完成,如有不当之处,欢迎指正,谢谢。

用Python操作sqlserver数据库时,要先安装用于Python链接sqlserver数据库的接口---pymssql

猜你喜欢

转载自blog.csdn.net/weixin_41752475/article/details/89441871