Trouble with MySQLdb

C

Chris

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
 
D

Dennis Lee Bieber

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)

Let's see... This implies cursors should default to dictionary
results, no?
def sql(self, query):
return self.cursor.execute(query)
No possibility of using a parameterized query?
def get(self, query, pkey = None):
self.sql(query)
if not pkey:
ret = []
for i in self.cursor:
ret.append(i)
return ret
Note that if pkey evaluates to False, you fall through and attempt
to return an undefined "ret". And if pkey evaluates to anything else you
then ignore it, initialize an empty "ret", followed by doing a loop
appending each returned record from the query one at a time... The same
thing that would be done with a simple

ret = self.cursor.fetchall()

def row(self, query):
self.sql(query)
for i in self.cursor:
return i
return None
And this loop is meaningless as you exit on the first row of data...
might as well use self.cursor.fetchone()
def field(self, query):
ret = []
self.sql(query)
for i in self.cursor:
ret.append(i[0])
return ret
This one predefines "ret" properly, but you defaulted to a
dictionary cursor, so indexing for the "first column" is going to fail
unless you have a column with a numeric "0" as the key (unlikely, since
field name in MySQL are not numbers).
def value(self, query):
self.sql(query)
for i in self.cursor:
return i[0]
return None
Here, again, you return the first "column" of the first record
only... no need for a for-loop; and probably won't work with dictionary
cursors.
def getFields(self, table):
rows = self.get("DESCRIBE `" + table + "`")
return [str(i[0]) for i in rows]
Note that a successful query typically populates a description list
in the cursor...

Your "abstraction layer" seems to add more complications than it
solves...

And sorry -- no idea of why you got the error... Suggest putting in
a few print statements or replicating the connection/cursor/query in
interactive mode...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top