pymssql 安装与 SQL Server连接
一、下载pymssql-2.1.0-py2.6-win32.egg (md5) ,放到python的script目录下,运行easy_install pymssql-2.1.0-py2.6-win32.egg (md5) 安装
二、http://pymssql.org/en/latest/pymssql_examples.html这里有连接的例子,一开始的时候照着也是连接不上,要打开SQL Server的网络连接。打开SQL Server的配置管理器,把网络配置里的TCP/IP启用。
三、中文的处理。要用encode('utf8'),如:commnad = commnad.encode('utf8')
官方例子:
from os importgetenv 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()