aidan aidan - 8 months ago 70
Python Question

Can't query SQL Server from django using django-pyodbc

I'm trying to sync an SQL Server 2008 R2 database running remotely on IIS 7, to a django 1.6 app running python 3.3 on Windows 7, using syncdb
. However I am being met with the error,

TypeError: The first argument to execute must be a string or unicode query.

I have django-pyodbc 0.2.3 and pyodbc 3.0.7 installed, with my

'default': {
'ENGINE': 'django_pyodbc',
'HOST': '...',
'NAME': '...',
'host_is_server': True

As you may guess,
are omitted since I need
for the connection.
appears to have to be non-empty due to the way django-pyodbc initialises the class
, even though
is irrelevant on Windows.

The full error I'm receiving is:

Traceback (most recent call last):
File "Z:\python\ns_reports_server\", line 10, in <module>
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\", line 399, in execute_from_command_line
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\", line 392, in execute
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\core\management\", 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\", 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\", 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\", line 57, in handle_noargs
cursor = connection.cursor()
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\", 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\", line 290, in _cursor
File "C:\Python33\lib\site-packages\django_pyodbc-0.2.3-py3.3.egg\django_pyodbc\", line 31, in _get_sql_server_ver
File "C:\Python33\lib\site-packages\django-1.6.1-py3.3.egg\django\db\backends\", 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\", 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\", line 410, in execute
TypeError: The first argument to execute must be a string or unicode query.

If you look at the source code for the tenth call from the top, django_pyodbc/ is querying the connection for the SQL Server version,

cur.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') as varchar)")

Yet, by the end of the call stack, this sql to be executed has disappeared. django_pyodbc/ has,

return self.cursor.execute(sql, params)

for which the first argument is not being recognised as a 'string or unicode query'.

Python, django, and SQL Server are all new for me so the answer might be obvious, but I can't for the life of me work it out. Cheers.


It looks like this is problem with django-pyodbc and Python 3.

In, line 367 is

sql = sql.encode('utf-8')

This line turns a string into bytes which is what is causing the TypeError. As you are on Windows, I am assuming the driver can handle unicode. I suggest you comment out lines 364 to 367 (the first 4 in the format_sql function). This way your unicode strings will stay unicode and you won't get the 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:
            sql = sql % tuple('?' * n_params)
            #Todo checkout whats happening here
        if '%s' in sql:
            sql = sql.replace('%s', '?')
    return sql

I have raised an issue with django-pyodbc which goes into a little more detail.