软件环境:

Windows 7 32bit

Python 3.6  Download https://www.python.org/downloads/

默认安装,并添加环境变量,一路Next ....

数据库: SQL Server2008 R2 Sp2 Express

==============================

使用Python Pip包管理工具:

运行cmd命令,切换到Python安装目录,

如:

C:\Program Files\Python36-\Scripts

进入目录,并执行命令安装Python所需要的包

 cd  C:\Program Files\Python36-\Scripts
easy_install pip
pip install pymssql

提示Error,需要 Microsoft Visual C++ 14.0,安装时,系统提示最低需要安装.NEt Framework4.5.1,继续安装之,

下载地址 :http://landinghub.visualstudio.com/visual-cpp-build-tools

继续重复安装SQL Server for Python库的安装 pip install pymssql,安装成功!

===============================================

报错:关于安装pymssql的坑!(Windows下)

安装pymssql模块包:

下载pymssql模块,从http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql找到!

如果不支持 whl包安装,则先安装pip install wheel,安装wheel工具!

===============================================

Python操作SQL Server 查询及更新操作(写入中文)

需要注意的是:读取数据的时候需要decode('utf-8'),写数据的时候需要encode('utf-8'),这样就可以避免烦人的中文乱码或报错问题。

Python操作SQLServer需要使用pymssql模块,使用pip install pymssql安装即可。

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

 # -*- 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'))

Python 连接 SQL Server示例,代码如下:

#-*- coding:GBK -*-

import pymssql
print 'Connect to the Datebase....' conn = pymssql.connect(host='10.0.1.5' ,user='lc0049999' ,password = '',database='drp') cur = conn.cursor()
if not cur:
raise(NameError,'connect failed') cur.execute('select * From lrkjqj') row = cur.fetchone()
print row
while row:
print row[0],row[1]
row = cur.fetchone()
conn.close()

  

==============================================

补充 Linux下安装 Pymssql数据库连接库

#安装pymssql,经常遇到异常报错,根本原因少一个依赖包,也就是:freetds-devel 坑爹,貌似是没有提示的。

一条龙安装如下:

  sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum -y install gcc gcc++ python-devel freetds-devel python-setuptools
easy_install pip
pip install pymssql mysql

install  Over!

Python 使用Microsoft SQL Server数据库的更多相关文章

  1. Microsoft SQL Server 数据库 错误号大全

    panchzh :Microsoft SQL Server 数据库 错误号大全0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒 ...

  2. Microsoft SQL Server 数据库

    1. master 数据库 master 数据库记录 SQL Server 系统的所有系统级别信息.它记录所有的登录帐户和系统配置设置.master 数据库是这样一个数据库,它记录所有其它的数据库,其 ...

  3. Jmeter—8 连接microsoft sql server数据库取数据

    本文以Jmeter 连接microsoft sql server为例. 1 从微软官网下载Microsoft SQL Server JDBC Driver 地址:http://www.microsof ...

  4. Jmeter入门8 连接microsoft sql server数据库取数据

    本文以Jmeter 连接microsoft sql server为例. 1 从微软官网下载Microsoft SQL Server JDBC Driver 地址:http://www.microsof ...

  5. 在Docker中使用Microsoft SQL Server数据库

    下图中对SQL Server容器创建及数据库创建等操作进行了记录,方便自己日后查看.(文中的 * 仅表示隐藏自己的个人信息,手动马赛克,哈哈-) Docker下载可看上一篇博文mac系统,docker ...

  6. Microsoft SQL Server 数据库服务器管理维护角色

    固定服务器角色: 按照从最低级别的角色(bulkadmin)到最高级别的角色(sysadmin)的顺序进行描述: Bulkadmin:这个服务器角色的成员可以运行BULK INSERT语句.这条语句允 ...

  7. Microsoft SQL Server数据库学习(一)

    数据库的分类: 1.关系型数据库: 数据库名称 类型 公司 平台 Access 小型数据库 微软 Windows Mysql 小型数据库 AB--sun--甲骨文 Windows/linux/mac ...

  8. Python批量插入SQL Server数据库

    因为要做性能测试,需要大量造数据到数据库中,于是用python写了点代码去实现,批量插入,一共四张表 简单粗暴地插入10万条数据 import pymssql import random __auth ...

  9. Microsoft SQL Server数据库语法

    目录   关于数据库的语法: 1.创建数据库 create database 数据库名on primary(主文件属性(name,filename,size等)) -用逗号隔开次要主要文件和次要文件( ...

随机推荐

  1. 454 Authenti cation failed, please open smtp flag first! (Net::SMTPAuthenticationError)

    在用ruby的smtp库发送邮件的时候,出现了这个错误454 Authenti cation failed, please open smtp flag first! (Net::SMTPAuthen ...

  2. CodeForces - 366C Dima and Salad (01背包)

    题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...

  3. MFC中Doc类获取View类的方法(SDI)

    从view类中获取Doc的方法如下: CYourDoc* pDoc = GetDocument(); 这个函数已经写好,所以无需自己添加,使用时直接利用pDoc即可. 若反过来,从Doc中获取View ...

  4. python安装mysql-python1.2.5

    首先安装好python 然后安装C++ Microsoft Visual C++ Compiler for Python 2.7 下载后双击安装 登录https://pypi.python.org/p ...

  5. SQL sqlserver order by 1,order by 后面直接加数字,多个字段排序

    ①select * from table order by n 表示select里面的第n个字段 ②多个字段排序

  6. python中 @property

    考察 Student 类: class Student(object): def __init__(self, name, score): self.name = name self.score = ...

  7. PHP搞笑注释代码-佛祖配美女

    //////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // / ...

  8. poj1942 Paths on a Grid(无mod大组合数)

    poj1942 Paths on a Grid 题意:给定一个长m高n$(n,m \in unsigned 32-bit)$的矩形,问有几种走法.$n=m=0$时终止. 显然的$C(m+n,n)$ 但 ...

  9. GOEXIF读取和写入EXIF信息

    最新版本的gexif,直接基于gdi+实现了exif信息的读取和写入,代码更清晰. /* * File: gexif.h * Purpose: cpp EXIF reader * 3/2/2017 & ...

  10. 学习java的一点体会

    在这几天的做实验.读书.写笔记的过程中我发现一个问题,就是我的知识面太窄,就比如说,学的知识都是一块一块的,没有能力去把它串起来,虽然学的很快,也写笔记总结,但马上就忘了,我想java是一个体系,需要 ...