开发者

Python, MySQL and a weird error

开发者 https://www.devze.com 2023-01-24 18:20 出处:网络
I have a bug that I don\'t know how to fix or even reproduce: query = \"SELECT id, nameFROM names ORDER BY id\"

I have a bug that I don't know how to fix or even reproduce:

query = "SELECT id, name  FROM names ORDER BY id"
results = database.execute(query)

where the class Database contains:

def execute(self, query):
    cursor = self.db.cursor()
    try:
        cursor.ex开发者_StackOverflow社区ecute(query)
        return cursor.fetchall()
    except:
        import traceback
        traceback.print_exc(file=debugFile)
        return []

This is how I open the database connection:

self.db = MySQLdb.connect(
    host=mysqlHost,
    user=mysqlUser,
    passwd=mysqlPasswd,
    db=mysqlDB
)

This is the stacktrace of the error:

File "foo.py", line 169, in application results = config.db.execute(query)
File "Database.py", line 52, in execute
     return cursor.fetchall()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 340, in fetchall
     self._check_executed()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 70, in _check_executed
     self.errorhandler(self, ProgrammingError, "execute() first")
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
     raise errorclass, errorvalue
ProgrammingError: execute() first

Do you have any ideas of why this is happening and how can I fix it? I searched on the internet and I found out that the reason may be having 2 cursors, but I have only one.


try this in your traceback it's for debugging:

except ProgrammingError as ex:
    if cursor:
        print "\n".join(cursor.messages) 
        # You can show only the last error like this.
        # print cursor.messages[-1]
    else:
        print "\n".join(self.db.messages)
        # Same here you can also do.
        # print self.db.messages[-1]
0

精彩评论

暂无评论...
验证码 换一张
取 消