python鏈接mssql

1、下載最新的pymssql並解壓python

         tar xzvf pymssql-2.1.0.tar.gzlinux

2、打開setup.py(假設freetds安裝路徑爲:/opt/local/freetds)sql

找到   usr_local = '/usr/local'服務器

 改成   usr_local = '/opt/local'測試

if osp.exists(usr_local):fetch

    add_dir_if_exists(ui

        include_dirs,spa

        osp.join(usr_local, 'include'),code

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

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

3、安裝pymssql模塊

即:python setup.py build

sudo python setup.py install

4、測試

$ 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()
相關文章
相關標籤/搜索