{"id":68,"date":"2013-06-15T01:09:37","date_gmt":"2013-06-15T01:09:37","guid":{"rendered":"https:\/\/code4reference.com\/?p=68"},"modified":"2023-10-01T13:34:59","modified_gmt":"2023-10-01T13:34:59","slug":"code4referencepython-mysqldb-cursor-error-code4reference","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=68","title":{"rendered":"Python MySQLdb cursor error."},"content":{"rendered":"<p>I had used MySQLdb in past to interface with MySQL database. Most of the time I had used <code>cursorclass=MySQLdb.cursors.DictCursor<\/code> for getting the result as dictionary but today I wanted to get the results in tuples so I used <code>cursorclass=MySQLdb.cursors.CursorTupleRowsMixIn<\/code>. But when I execute it, it failed and complained about cursor() method call. The exception is as following.<\/p>\n<pre>\nFile \"testdata.py\", line 108, in main()\n dbCursor = db.cursor(cursorclass=MySQLdb.cursors.CursorTupleRowsMixIn)\n File \"\/usr\/lib\/python2.7\/dist-packages\/MySQLdb\/connections.py\", line 243, in cursor\n return (cursorclass or self.cursorclass)(self)\n<\/pre>\n<p>I was confused because I know for sure that the following code was working fine on the other machine.<\/p>\n<pre>\nimport MySQLdb\ndb = MySQLdb.connect(host=options.dbHost, user = options.dbUser, passwd = options.dbPassword, cursorclass=MySQLdb.cursors.CursorTupleRowsMixIn)\ndbCursor = db.cursor()\n<\/pre>\n<p>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 <code>print MySQLdb.__version__<\/code> 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\u2019t get any correct one. Then I wasn\u2019t left with any option except hit-and-trial method. I tried to remove the <code>cursorclass<\/code> and it worked like charm. And it did return the result in tuples.<\/p>\n<pre>\nimport MySQLdb\ndb = MySQLdb.connect(host=options.dbHost, user = options.dbUser, passwd = options.dbPassword)\ndbCursor = db.cursor()\n<\/pre>\n<p>Hope this helped you to fix your problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":1,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}