Bootstrap

python连接mssql

一、下载最新的pymssql并解压

         tar xzvf pymssql-2.1.0.tar.gz

二、打开setup.py(假设freetds安装路径为:/opt/local/freetds)

找到   usr_local = '/usr/local'

 改为   usr_local = '/opt/local'

if osp.exists(usr_local):

    add_dir_if_exists(

        include_dirs,

        osp.join(usr_local, 'include'),

        osp.join(usr_local, 'include/freetds'),

        osp.join(usr_local, 'freetds/include')

    )

    add_dir_if_exists(

        library_dirs,

        osp.join(usr_local, 'lib'),

        osp.join(usr_local, 'lib/freetds'),

        osp.join(usr_local, 'freetds/lib')

改为

if osp.exists(usr_local):

    add_dir_if_exists(

        include_dirs,

        osp.join(usr_local, 'freetds/include'),

        osp.join(usr_local, 'include/freetds'),

        osp.join(usr_local, 'include')

    )

    add_dir_if_exists(

        library_dirs,

        osp.join(usr_local, 'freetds/lib'),

        osp.join(usr_local, 'lib/freetds'),

        osp.join(usr_local, 'lib')

三、安装pymssql模块

即:python setup.py build

sudo python setup.py install

四、测试

$ python

Python 2.7.6 (default, Mar 22 2014, 22:59:56) 

[GCC 4.8.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import pymssql

>>> conn =  pymssql.connect(host='egServer70',user='you_name',password='you_passwd',database='y_data',charset="utf8")

egServer70是freetds.conf中配置的服务名,也可以用mssql服务器的地址

#coding=utf-8
import pymssql

conn =  pymssql.connect(host='egServer70',user='mymotif',password='wxwpxh',database='mymotif',charset="utf8")
cur = conn.cursor() 			

cur.execute('select * from STUDENT')
rows = cur.fetchall()
for row in rows:
	print "-" * 55
	for col in range (len(row)):
		print "%s" % (row[col])
	
cur.close()
conn.close()


转载于:https://my.oschina.net/u/2245781/blog/636508

;