在项目可能需要sql server,顺便实验了下能否和django集成
总体分三步:
1 安装sql server 2005 并且在odbc配置中测试
遇到1433端口,服务启动等各种问题,参考网上文章解决。
SQL Server ODBC数据源连接失败问题总结 http://wenku.baidu.com/view/ff191749f7ec4afe04a1dfe6.html
2 安装pyodbc,在命令行中测试
2.1、下载并安装pyodbc
下载地址:http://code.google.com/p/pyodbc/downloads/list
2.2、访问SqlServer
>>> import pyodbc
>>>cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.1.100\\sql;DATABASE=testDB;UID=sa;PWD=myPassword')
>>>cursor = cnxn.cursor()
>>>cursor.execute("select * from Tb")
在这里碰到很奇怪的问题,用DRIVER={SQL Server}无法打开,很慢会报错,但是DRIVER={SQL Native Client}就没有问题,而且很快。
3 测试django-pyodbc,并在django中测试
3.1安装django-pyodbc 下载地址 http://code.google.com/p/django-pyodbc/source/checkout
3.2 在django中测试
参考 http://blog.csdn.net/omage/article/details/7312919
但是如果按照其配置,仍然不通,各种错误:
Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]\xce\xde\xd0\xa7\xb5\xc4\xc1\xac\xbd\xd3\xa1\xa3 (14) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Invalid Instance()). (14)')
Error: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver][Shared Memory]\xce\xde\xd0\xa7\xb5\xc4\xc1\xac\xbd\xd3\xa1\xa3 (14) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][Shared Memory]ConnectionOpen (Invalid Instance()). (14)')
Error: ('28000', '[28000] [Microsoft][SQL Native Client][SQL Server]\xd3\xc3\xbb\xa7 \'sa\' \xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (18456) (SQLDriverConnect); [42000] [Microsoft][SQL Native Client][SQL Server]\xce\xde\xb7\xa8\xb4\xf2\xbf\xaa\xb5\xc7\xc2\xbc\xcb\xf9\xc7\xeb\xc7\xf3\xb5\xc4\xca\xfd\xbe\xdd\xbf\xe2 "test"\xa1\xa3\xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (4060); [28000] [Microsoft][SQL Native Client][SQL Server]\xd3\xc3\xbb\xa7 \'sa\' \xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (18456); [42000] [Microsoft][SQL Native Client][SQL Server]\xce\xde\xb7\xa8\xb4\xf2\xbf\xaa\xb5\xc7\xc2\xbc\xcb\xf9\xc7\xeb\xc7\xf3\xb5\xc4\xca\xfd\xbe\xdd\xbf\xe2 "test"\xa1\xa3\xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (4060)')
Traceback (most recent call last):
File "
cnxn = pyodbc.connect('DRIVER={SQL Native Client};SERVER=localhost;DATABASE=ren-pc\test;UID=sa;PWD=admin')
Error: ('28000', '[28000] [Microsoft][SQL Native Client][SQL Server]\xd3\xc3\xbb\xa7 \'sa\' \xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (18456) (SQLDriverConnect); [42000] [Microsoft][SQL Native Client][SQL Server]\xce\xde\xb7\xa8\xb4\xf2\xbf\xaa\xb5\xc7\xc2\xbc\xcb\xf9\xc7\xeb\xc7\xf3\xb5\xc4\xca\xfd\xbe\xdd\xbf\xe2 "ren-pc\test"\xa1\xa3\xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (4060); [28000] [Microsoft][SQL Native Client][SQL Server]\xd3\xc3\xbb\xa7 \'sa\' \xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (18456); [42000] [Microsoft][SQL Native Client][SQL Server]\xce\xde\xb7\xa8\xb4\xf2\xbf\xaa\xb5\xc7\xc2\xbc\xcb\xf9\xc7\xeb\xc7\xf3\xb5\xc4\xca\xfd\xbe\xdd\xbf\xe2 "ren-pc\test"\xa1\xa3\xb5\xc7\xc2\xbc\xca\xa7\xb0\xdc\xa1\xa3 (4060)')
实在无语,研究了下django-pyodbc的源码,尤其是sql_server目录下代码,发现在base.py中有如下代码:
else:
if os.name == 'nt':
driver = 'SQL Server'
else:
driver = 'FreeTDS'
猜测原因应该和上一步一样,所以修改如下:
else:
if os.name == 'nt':
#driver = 'SQL Server'
driver = 'SQL Native Client'
else:
driver = 'FreeTDS'
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes