6

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

alfadog67
  • 831
  • 7
  • 28
  • 1
    I cannot recreate any issue. No warning and no empty `aryRes`. Do note: the password argument in `pymysql.connect` is **passwd**. – Parfait Dec 17 '16 at 01:02
  • Nice catch! I'll fix it now. I changed my code here as well, but it didn't affect my situation - probably because my db password is empty. – alfadog67 Dec 17 '16 at 01:43
  • I wanted to reassure myself... You did change the DictCursor to SSCursor in the top example, right? – alfadog67 Dec 17 '16 at 01:46
  • 1
    Both cursor classes worked for me for top and bottom examples. – Parfait Dec 17 '16 at 02:12
  • I left one thing out of my sample code. After self.cursor.execute(), I had self.conn.commit(), which fails for SELECT queries. – alfadog67 Dec 24 '16 at 18:58
  • I thought `SScursor` doesn't need `conn.commit()`. I have problem like this. it disappear when I remove `conn.commit()` after `cursor.execute()` – Yuda Prawira Sep 09 '17 at 23:08

1 Answers1

1

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

Fejs
  • 2,734
  • 3
  • 21
  • 40
  • Well, it's worth a shot, but a couple of things come to mind... 1) I don't need to fetchall() in the non-encapsulated version, and 2) the query itself is written to return one record. – alfadog67 Dec 17 '16 at 00:33
  • Ok I tried it, and instead of returning None, it returned an empty array, and the error message persisted. – alfadog67 Dec 17 '16 at 00:39