Python, pymysql class encapsulation of SSCursor not working as expected

2.1k views Asked by At

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

1

There are 1 answers

2
Fejs On

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