I had used MySQLdb in past to interface with MySQL database. Most of the time I had used
cursorclass=MySQLdb.cursors.DictCursor for getting the result as dictionary but today I wanted to get the results in tuples so I used
cursorclass=MySQLdb.cursors.CursorTupleRowsMixIn. But when I execute it, it failed and complained about cursor() method call. The exception is as following.
File "testdata.py", line 108, in main() dbCursor = db.cursor(cursorclass=MySQLdb.cursors.CursorTupleRowsMixIn) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 243, in cursor return (cursorclass or self.cursorclass)(self)
I was confused because I know for sure that the following code was working fine on the other machine.
import MySQLdb db = MySQLdb.connect(host=options.dbHost, user = options.dbUser, passwd = options.dbPassword, cursorclass=MySQLdb.cursors.CursorTupleRowsMixIn) dbCursor = db.cursor()
I doubted the python version, my older machine had 2.6.5 whereas my new machine has 2.7. This gave me enough lead to suspect the library version. Then I check
print MySQLdb.__version__ I got 1.2.2 on older one whereas on new machine I got 1.2.3. Definitely it was issue with the version. I tried to get the pydoc but I couldn’t get any correct one. Then I wasn’t left with any option except hit-and-trial method. I tried to remove the
cursorclass and it worked like charm. And it did return the result in tuples.
import MySQLdb db = MySQLdb.connect(host=options.dbHost, user = options.dbUser, passwd = options.dbPassword) dbCursor = db.cursor()
Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below. You can find Facebook page here.