linux 下pymssql模块的安装

所需压缩包:
pymssql-2.1.0.tar.bz2
freetds-patched.tar.gz

安装:

tar -xvf pymssql-2.1.0.tar.bz2
cd pymssql-2.1.0
python setup.py install
报错 则需要安装freetds

tar -zxvf freetds-patched.tar.gz
cd freetds-0.95.87/
mkdir /usr/local/freetds
./configure --prefix=/usr/local/freetds --with-tdsver=7.0 --enable-msdblib --disable-libiconv --host=arm-none-linux-gnueabi
make && make install
再次安装pymssql时提示缺少 **.so.5之类的文件 则需
yum install compat-libstdc++-33.i686
之后再进行pymssql的安装即可成功
测试:
python
import pymssql

python 连接sql server的简单封装例子(执行查询更新操作(写入中文))

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('','')") 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()

python 连接sql server的例子:

import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase')
cur = conn.cursor()
cur.execute('CREATE TABLE persons(id INT, name VARCHAR(100))')
cur.executemany("INSERT INTO persons VALUES(%d, xinos.king)", \
[ (1, 'John Doe'), (2, 'Jane Doe') ])
conn.commit() # you must call commit() to persist your data if you don't set autocommit to True cur.execute('SELECT * FROM persons WHERE salesrep=xinos.king', 'John Doe')
row = cur.fetchone()
while row:
print "ID=%d, Name=xinos.king" % (row[0], row[1])
row = cur.fetchone() # if you call execute() with one argument, you can use % sign as usual
# (it loses its special meaning).
cur.execute("SELECT * FROM persons WHERE salesrep LIKE 'J%'") conn.close()

其中可能涉及的小知识:

游标:

cu = conn.cursor()

能获得连接的游标,这个游标可以用来执行SQL查询。

 conn.commit()

完成插入并且做出某些更改后确保已经进行了提交,这样才可以将这些修改真正地保存到文件中。

游标对象方法:

fetchall()

返回结果集中的全部数据,结果为一个tuple的列表。每个tuple元素是按建表的字段顺序排列。注意,游标是有状态的,它可以记录当前已经取到结果的第几个记录了,因此,一般你只可以遍历结果集一次。在上面的情况下,如果执行fetchone()会返回为空。这一点在测试时需要注意

python 连接sql server的更多相关文章

  1. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  2. python 连接 SQL Server 数据库

    #!/usr/bin/python # -*- coding:utf-8 -*- import pymssql import pyodbc host = '127.0.0.1:1433' user = ...

  3. 函数计算 Python 连接 SQL Server 小结

    python 连接数据库通常要安装第三方模块,连接 MS SQL Server 需要安装 pymssql .由于 pymsql 依赖于 FreeTDS,对于先于 2.1.3 版本的 pymssql,需 ...

  4. python 连接sql server数据库的示例代码

    首先,到http://pymssql.sourceforge.net/下载pymssql模块,必须安装这个模块才可以用python连接mysql 以下是sql server的操作代码,需要注意字符集 ...

  5. Python 学习笔记:Python 连接 SQL Server 报错(20009, b'DB-Lib error message 20009, severity 9)

    问题及场景: 最近需要使用 Python 将数据写到 SQL Server 数据库,但是在进行数据库连接操作时却报以下错误:(20009, b'DB-Lib error message 20009, ...

  6. python入门23 pymssql模块(python连接sql server增删改数据 )

    增删改数据必须connect.commit()才会生效 回滚函数 connect.rollback() 连接数据库 ''' dinghanhua sql server增删改 ''' import py ...

  7. python连接sql server数据库

    记录一下pyodbc连接数据库的使用方法和注意事项,基于python2.7:  前提: pip install pyodbc  .下载pyodbc包.   pyodbc.connect('DRIVER ...

  8. Python连接SQL Server数据库 - pymssql使用基础

    连接数据库 pymssql连接数据库的方式和使用sqlite的方式基本相同: 使用connect创建连接对象 connect.cursor创建游标对象,SQL语句的执行基本都在游标上进行 cursor ...

  9. python 连接SQL SERVER 并读取其数据

    1.没什么难的操作 安装  pip install pymssql import pymssql #引入pymssql模块 import pandas as pd def conn(): connec ...

随机推荐

  1. Android 中 SQLite 数据库的查看

    当 SQLite 数据库创建完成后,如何查看数据库的内容呢?如果直接使用 File Explorer 查看,最多只能看到 database 目录下出现了一个 BookStore.db 文件,Book ...

  2. 借助 SublimeLinter 编写高质量的 JavaScript & CSS 代码

    SublimeLinter 是前端编码利器——Sublime Text 的一款插件,用于高亮提示用户编写的代码中存在的不规范和错误的写法,支持 JavaScript.CSS.HTML.Java.PHP ...

  3. TextMate 小小心得

    在Vim.Emacs之间纠结了很久之后,却选择了TextMate P.S. 为何Emacs和Vim被称为两大神器 中文的资料不是很多,一狠心,找了James Edward Gray II的TextMa ...

  4. php foreach循环中unset后续的键值问题

    实例: $arr=array('a','b','c','d','e','f'); foreach($arr as $index=>$tmp){ echo $index.'=>'.$tmp. ...

  5. question2answer论坛框架分析及web开发思考

    2015年7月25日 17:31:42 星期六 一个专门做论坛的开源PHP框架, 有后台, 支持多语种 入口文件是框架根目录的index.php 他包含了/qa-include/qa-index.ph ...

  6. bing壁纸xml地址

    http://www.bing.com/gallery/?src=livesino# http://www.bing.com/HPImageArchive.aspx?format=xml&id ...

  7. [ 转]Collections.unmodifiableList方法的使用与场景

    在公司接触到Collections.unmodifiableList(List<? extends T> list)) 觉得用法挺特殊的,所以学习了下,简单而言,看名字就知道,将参数中的L ...

  8. BestCoder8 1001.Summary(hdu 4989) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...

  9. jquery.base64.js简单使用

    jquery.base64.js, 加密,使用,先引入jquery,然后引入jquery.base64.js 使用如下 js中加密如下 $.base64.encode(result[i].ipadre ...

  10. 【leetcode】 Search a 2D Matrix (easy)

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...