executing raw sql

1,224 views
Skip to first unread message

Larry Martell

unread,
Apr 30, 2012, 2:46:34 PM4/30/12
to django...@googlegroups.com
I'm trying to execute some raw sql. I found some code that did this:

from django.db import connection
cursor = connection.cursor()
cursor.execute(sql)
data = cursor.fetchall()

But the cursor.execute(sql) is blowing up with:

'Cursor' object has no attribute '_last_executed'

What is the best or proper way for me to execute my raw sql?

Larry Martell

unread,
Apr 30, 2012, 4:32:46 PM4/30/12
to django...@googlegroups.com
I traced this with the debugger, and it's blowing up in
django/db/backends/mysql/base.py in this:

def last_executed_query(self, cursor, sql, params):
# With MySQLdb, cursor objects have an (undocumented) "_last_executed"
# attribute where the exact query sent to the database is saved.
# See MySQLdb/cursors.py in the source distribution.
return cursor._last_executed

Could this have something to do with the version of django and/or mysql?

I'm running django 1.5 and MySQL 5.5.19, and MySQLdb 1.2.3. I just
tried this and that does not exist on my system:

$ python
Python 2.6.7 (r267:88850, Jan 11 2012, 06:42:34)
[GCC 4.0.1 (Apple Inc. build 5490)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> conn = MySQLdb.connect(host, user, passwd, db)
>>> cursor = conn.cursor()
>>> print cursor._last_executed
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Cursor' object has no attribute '_last_executed'
>>> print cursor.__dict__
{'_result': None, 'description': None, 'rownumber': None, 'messages':
[], '_executed': None, 'errorhandler': <bound method
Connection.defaulterrorhandler of <_mysql.connection open to
'localhost' at 889810>>, 'rowcount': -1, 'connection': <weakproxy at
0x62f630 to Connection at 0x889810>, 'description_flags': None,
'arraysize': 1, '_info': None, 'lastrowid': None, '_warnings': 0}
Message has been deleted

Larry Martell

unread,
Apr 30, 2012, 5:29:08 PM4/30/12
to django...@googlegroups.com
I've found that cursor._last_executed doesn't exist until a query has
been executed (it's not initialized to None)

But in django I am doing this:

from django.db import connection
cursor = connection.cursor()
cursor.execute(sql)

and it's getting the error, so that would mean that the sql isn't
getting executed for some reason. I'll have to investigate that
avenue.
Message has been deleted

Larry Martell

unread,
Apr 30, 2012, 8:38:09 PM4/30/12
to django...@googlegroups.com
On Mon, Apr 30, 2012 at 6:26 PM, Dennis Lee Bieber
<wlf...@ix.netcom.com> wrote:
> On Mon, 30 Apr 2012 15:29:08 -0600, Larry Martell
> <larry....@gmail.com> declaimed the following in
> gmane.comp.python.django.user:
>
>>
>> But in django I am doing this:
>>
>> from django.db import connection
>> cursor = connection.cursor()
>> cursor.execute(sql)
>>
>> and it's getting the error, so that would mean that the sql isn't
>> getting executed for some reason. I'll have to investigate that
>> avenue.
>
>        You still haven't show us what "sql" contains... Given your three
> statements I'd expect to get an error message since "sql" itself is
> undefined.

I figured out the problem. I had %s in my sql, and they was getting
treated as a format specifier. When I built the sql I did put in '%%',
but I had to put in 4 '%%%%' for it to work.
Reply all
Reply to author
Forward
0 new messages