Trouble with MySQLdb

Chris cdbarlow at gmail.com
Tue Sep 15 14:44:06 EDT 2009


I'm receiving the following error:



Traceback (most recent call last):
  File "db.py", line 189, in <module>
    rows = db.get("SELECT * FROM survey")
  File "db.py", line 55, in get
    self.sql(query)
  File "db.py", line 47, in sql
    return self.cursor.execute(query)
  File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 168, in
execute
  File "build/bdist.linux-i686/egg/MySQLdb/cursors.py", line 73, in
_warning_check
SystemError: null argument to internal routine



Here's the source for the db.py file:



#!/usr/bin/env python

import MySQLdb
import MySQLdb.cursors

####################################################################################################

#
# MySQL Abstraction Layer
#

class MySQL:
    "MySQL Abstraction Layer"
    connection  = None
    cursor      = None
    db          = None

    def __init__(self, host = 'localhost', usr = 'user', pwd = 'pass',
db = 'main'):
        if host:
            self.connect(host, usr, pwd, db)

    def connect(self, host, usr, pwd, db):
        self.connection = MySQLdb.connect(host, usr, pwd, db,
            unix_socket='/opt/lampp/var/mysql/mysql.sock',
            cursorclass = MySQLdb.cursors.DictCursor)
        self.cursor = self.connection.cursor()
        self.db = db

    ##############################

    def sql(self, query):
        return self.cursor.execute(query)

    def next(self):
        return self.cursor.fetchone()

    ##############################

    def get(self, query, pkey = None):
        self.sql(query)
        if not pkey:
            ret = []
            for i in self.cursor:
                ret.append(i)
        return ret

    def row(self, query):
        self.sql(query)
        for i in self.cursor:
            return i
        return None

    def field(self, query):
        ret = []
        self.sql(query)
        for i in self.cursor:
            ret.append(i[0])
        return ret

    def value(self, query):
        self.sql(query)
        for i in self.cursor:
            return i[0]
        return None

    ##############################

    def getFields(self, table):
        rows = self.get("DESCRIBE `" + table + "`")
        return [str(i[0]) for i in rows]

    def getPrimaryKey(self, table):
        rows = self.get("DESCRIBE `" + table + "`")
        return [str(i[0]) for i in rows if i[3] == 'PRI']

####################################################################################################

db = MySQL()
rows = db.get("SELECT * FROM survey")

for i in rows:
    print i

raise SystemExit



More information about the Python-list mailing list