通过pyODBC 连接mssql 配置后无法连接

widows环境下
我用pyodbc连接mssql , pyodbc版本为:pyodbc-2.1.8.win32-py2.7.exe python为:2.7

pyodbc安装到python目录成功后,连接代码如下

[code="java"]
import urllib2
from BeautifulSoup import BeautifulSoup,Tag
import re
import os
import stat
#import MySQLdb
import pyodbc

source = 'DRIVER={SQL Server};SERVER=.;DATABASE=test;UID=sa;PWD=123456'
db = pyodbc.connect(source)

cursor = db.cursor()

cursor.execute('select url,term from html_url where term="103"')

for i in cursor.fetchall():
url_str=str(i[0])
print url_str

[/code]

报错信息:
[img]http://dl.iteye.com/upload/attachment/477571/87c7fb22-72a2-3c37-a2db-e8f50a656f18.jpg[/img]

请问可能是什么原因造成的, 感激不尽

1个回答

是不是pyodbc没有安装成功啊?
在你的python的Lib\site-packages目录下边有pyodbc.pyd这个文件吗?
如果安装成功,
你可以试一下最简单的例子:

[code="java"]#!/usr/bin/python

coding=gbk

"""
Mainly used to manipulate the common database.
"""
import pyodbc

"""
return the datasource list of local machine
"""
def listDSNs():
return pyodbc.dataSources()

if name == '__main__':
#src = 'DRIVER={SQL Server};SERVER=.;DATABASE=test;UID=sa;PWD=123456'
src = 'DSN=sampledb;UID=dba;pwd=sql'
db = pyodbc.connect(src)
cursor = db.cursor()
cursor.execute('select 1')

for i in cursor.fetchall():

url_str=str(i[0])
print url_str
db.close()[/code]

立即提问