WIN下安装PYMSSQL,由于我没有系统管理权限,无法安装,
那只好在LINUX下面安装罗。。
以下这个文章帮助我搞定。
http://blog.csdn.net/five3/article/details/16338191
各版本的下载地址:https://pypi.python.org/pypi/pymssql/
Windows可以下载installer文件,直接是编译好的,可以直接安装
Linux下需要安装几个基础类库:
Cpython:pip install Cpython ##Python包
freetds-dev:yum install freetds-devel.x86_64 / apt-get install freetds-dev ##linux包
最后安装pymssql: pip install pymssql
import pymssql conn = pymssql.connect(host=host, user=user, password=password, database=database, charset=', as_dict=False) cursor = conn.cursor() sql = "select * from payment where id in (59,60)" cursor.execute(sql) results = cursor.fetchall() for row in results: print row
from os import getenv import pymssql server = getenv("PYMSSQL_TEST_SERVER") user = getenv("PYMSSQL_TEST_USERNAME") password = getenv("PYMSSQL_TEST_PASSWORD") conn = pymssql.connect(server, user, password, "tempdb") cursor = conn.cursor() cursor.execute(""" IF OBJECT_ID('persons', 'U') IS NOT NULL DROP TABLE persons CREATE TABLE persons ( id INT NOT NULL, name VARCHAR(100), salesrep VARCHAR(100), PRIMARY KEY(id) ) """) cursor.executemany( "INSERT INTO persons VALUES (%d, %s, %s)", [(1, 'John Smith', 'John Doe'), (2, 'Jane Doe', 'Joe Dog'), (3, 'Mike T.', 'Sarah H.')]) # you must call commit() to persist your data if you don't set autocommit to True conn.commit() cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe') row = cursor.fetchone() while row: print("ID=%d, Name=%s" % (row[0], row[1])) row = cursor.fetchone() conn.close()