1

我从以下位置收到此错误views.py

('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (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 文件

谢谢

4

1 回答 1

0

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

尝试这样的事情:

connstr = 'DRIVER={SQL Server};SERVER=dev_appserver;DATABASE=DemoApp;UID=username;PWD=password'
conn = pyodbc.connect(connstr)
于 2013-06-14T18:56:02.190 回答