我正在尝试将一个在IIS 7上远程运行的Server 2008 R2数据库同步到一个在Windows7上运行python3.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.py DATABASES为,
{
'default': {
'ENGINE': 'django_pyodbc',
'HOST': '...',
'NAME': '...',
'OPTIONS': {
'host_is_server': True
}
}
}正如您可能猜到的,由于连接需要USER和Trusted_Connection=Yes,所以省略了Integrated_Security=Yes和Trusted_Connection=Yes。由于django-pyodbc初始化类OPTIONS的方式,host_is_server似乎必须是非空的,尽管host_is_server与host_is_server无关。
我收到的全部错误是:
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.如果您从顶部查看第十个调用的源代码,pyodbc/operations.py将查询Server版本的连接,
cur.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') as varchar)")然而,到调用堆栈结束时,要执行的sql已经消失。pyodbc/base.py有,
return self.cursor.execute(sql, params)第一个参数不被识别为'string或unicode查询‘。
Python、django和SQL Server对我来说都是新的,所以答案可能是显而易见的,但我无法在我的生命中解决它。干杯。
发布于 2014-01-25 21:53:58
编辑:在其他答案中提到的driver_supports_utf8=True将是正确的解决方法。
看起来这是django-pyodbc和Python 3的问题。
在base.py中,第367行是
sql = sql.encode('utf-8')这一行将字符串转换为字节,这就是导致TypeError的原因。由于您在Windows上,我假设驱动程序可以处理unicode。我建议您将第364至367行( format_sql函数中的前4行)注释掉。这样,您的unicode字符串将保持unicode,您将无法获得TypeError。
def format_sql(self, sql, n_params=None):
if not self.driver_supports_utf8 and isinstance(sql, text_type):
# Older FreeTDS (and other ODBC drivers?) don't support Unicode yet, so
# we need to encode the SQL clause itself in utf-8
sql = sql.encode('utf-8')
# pyodbc uses '?' instead of '%s' as parameter placeholder.
if n_params is not None:
try:
sql = sql % tuple('?' * n_params)
except:
#Todo checkout whats happening here
pass
else:
if '%s' in sql:
sql = sql.replace('%s', '?')
return sql我已经向django-pyodbc提出了一个问题,这将涉及到更多的细节。
发布于 2014-05-07 20:55:43
您也可以在您的配置选项中修复此问题。我和这个吵了一会。尝试将您的DB更改为be config,如下所示,用于1.6:
DATABASES = {
'default': {
'ENGINE': 'django_pyodbc',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'your_password',
'HOST': 'database.domain.com,1433',
'PORT': '1433',
'OPTIONS': {
'host_is_server': True,
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0'
},
}
}如果您在Windows上,"extra_params“将被忽略,但它使其可移植到Linux。
发布于 2016-04-25 19:25:09
这个问题通过添加'driver_supports_utf8': True,'为我们解决了
'characteristics': {
'ENGINE': "django_pyodbc",
'HOST': "ourdbhost.com",
'PORT': 5555,
'NAME': "OurDB",
'USER': "ouruser",
'PASSWORD': "acoolpw",
'OPTIONS': {
'driver_supports_utf8': True,
'host_is_server': True,
'extra_params': 'TDS_Version=7.1',
},
},按gri在https://github.com/lionheart/django-pyodbc/issues/47上的建议工作
添加
'autocommit': True,
'unicode_results': True,OPTIONS没有修复它,但也没有破坏任何东西。
https://stackoverflow.com/questions/21272895
复制相似问题