我正在尝试将在 IIS 7 上远程运行的 SQL Server 2008 R2 数据库同步到在 Windows 7 上运行 python 3.3 的 django 1.6 应用程序,使用manage.py syncdb
. 但是我遇到了错误,
TypeError: The first argument to execute must be a string or unicode query.
我安装了 django-pyodbc 0.2.3 和 pyodbc 3.0.7,我的 settings.pyDATABASES
为,
{
'default': {
'ENGINE': 'django_pyodbc',
'HOST': '...',
'NAME': '...',
'OPTIONS': {
'host_is_server': True
}
}
}
正如您可能猜到的那样,USER
并且PASSWORD
由于我需要Integrated_Security=Yes
和Trusted_Connection=Yes
连接而被省略。OPTIONS
由于 django-pyodbc 初始化 class 的方式,似乎必须是非空的DatabaseWrapper
,即使host_is_server
在 Windows 上是不相关的。
我收到的完整错误是:
Traceback (most recent call last):
File "Z:\python\ns_reports_server\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\base.py", line 285, in execute
output = self.handle(*args, **options)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\base.py", line 415, in handle
return self.handle_noargs(**options)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\commands\syncdb.py", line 57, in handle_noargs
cursor = connection.cursor()
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\__init__.py", line 157, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\base.py", line 290, in _cursor
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\operations.py", line 31, in _get_sql_server_ver
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\util.py", line 51, in execute
return self.cursor.execute(sql)
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\base.py", line 410, in execute
TypeError: The first argument to execute must be a string or unicode query.
如果您查看从顶部开始第十次调用的源代码,django_pyodbc/operations.py正在查询 SQL Server 版本的连接,
cur.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') as varchar)")
然而,到调用堆栈结束时,这条要执行的sql已经消失了。django_pyodbc/base.py有,
return self.cursor.execute(sql, params)
第一个参数未被识别为“字符串或 Unicode 查询”。
Python、django 和 SQL Server 对我来说都是新的,所以答案可能很明显,但我一辈子都无法解决。干杯。