Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, pymysql class encapsulation of SSCursor not working as expected

The following works:

class DB():
    def __init__(self, host, user, password, db):
        self.conn =  pymysql.connect(
            host = host,
            user = user,
            passwd = password,
            charset = 'utf8',
            cursorclass = pymysql.cursors.DictCursor,
            db = db
        )

        self.cur = self.conn.cursor()

    def execute(self, sql):
        self.cur.execute(sql)

    def fetchone(self):
        return self.cur.fetchone()

cf = DB("localhost", "user", "pass", "db")
cf.execute("SELECT Count(*) FROM Table")
aryRes = cf.fetchone()

But when I replace the DictCursor with an SSCursor, aryRes contains None, and I get:

warnings.warn("Previous unbuffered result was left incomplete")

I've tried all the combinations of:

import pymysql
import pymysql.cursors
from pymysql import cursors

I've also confirmed that the following returns correctly (Not encapsulated):

con = pymysql.connect(
    host = "localhost", 
    user = "user",
    passwd = "pass",
    charset = 'utf8',
    cursorclass = pymysql.cursors.SSCursor,
    db = "db"
)
cur = con.cursor()
cur.execute("SELECT Count(*) FROM Table")
aryRes = cur.fetchone()

What am I missing that will make encapsulated SSCursors work? I'm new to Python, so feel free to correct anything you feel needs attention.

Forgot to mention... I'm using Python 2.7.12

like image 930
alfadog67 Avatar asked Dec 16 '16 23:12

alfadog67


1 Answers

I think You have problem because You fetch just one result, while there are others. After fetchone, try running fetchall to clear buffer.

like image 162
Fejs Avatar answered Oct 06 '22 00:10

Fejs