使用unixODBC连接时“找不到数据源名称”

时间:2013-04-03 17:19:04

标签: sql-server django unixodbc

我从views.py收到此错误:

  

('IM002','[IM002] [unixODBC] [Driver Manager]数据源名称没有   找到,并且没有指定默认驱动程序(0)(SQLDriverConnectW)')

这是views.py将数据库数据保存到json文件

connstr = 'DRIVER={SQL Server};SERVER=dev_appserver;DATABASE=DemoApp;'
conn = pyodbc.connect(connstr)
cursor = conn.cursor()

cursor.execute("""
            SELECT book_id, book_name, author_name, publisher_name, email, bookref
            FROM Book
            """)

rows = cursor.fetchall()


rowarray_list = []
for row in rows:
    t = (row.book_id, row.book_name, row.author_name, row.publisher_name, 
         row.email, row.bookref)
    rowarray_list.append(t)

j = json.dumps(rowarray_list)
rowarrays_file = 'student_rowarrays.json'
f = open(rowarrays_file,'w')
print >> f, j

objects_list = []
for row in rows:
    d = collections.OrderedDict()
    d['book_id'] = row.book_id
    d['book_name'] = row.book_name
    d['author_name'] = row.author_name
    d['publisher_name'] = row.publisher_name
    d['email'] = row.email
    d['bookref'] = row.bookref
    objects_list.append(d)

j = json.dumps(objects_list)
objects_file = 'student_objects.json'
f = open(objects_file,'w')
print >> f, j

conn.close()

编码将数据库数据写入json文件

由于

1 个答案:

答案 0 :(得分:0)

连接数据库时,您似乎没有传递用户名或密码。

尝试这样的事情:

connstr = 'DRIVER={SQL Server};SERVER=dev_appserver;DATABASE=DemoApp;UID=username;PWD=password'
conn = pyodbc.connect(connstr)