[Tutor] python-mysql

Bernard Lebel python at bernardlebel.com
Mon Aug 16 09:54:11 CEST 2004

Hi again,

I have another question.
I managed to properly install the module, and it works, because I can
connect to the database, I can create an object, and I can even send a query
and fetch the result.

However this time the problem is that when I used to use VBScript and
JScript to do the same thing with an activeX object, I could send a second
query using arguments obtained from the first, but now it doesn't and I'm
not sure why. Here is my code:

# Import module
import MySQLdb

# Create instance
sql = MySQLdb

# host (server)
sHost = 'xxx.xxx.x.x'

# uid (user)
sUser = 'xsi_client'

# pwd (password)
sPwd = 'XSIBatchServe'

# database
sDB = 'XSI_R'

# Create connection object
oDB = sql.connect( sHost, sUser, sPwd , sDB )

iID = 31
sID = str( iID )

# Execute first query
oCursor.execute( ' SELECT from_rule, where_rule, order_rule ' + ' FROM
clients LEFT JOIN rules ON rules.id=clients.rule_id' + ' WHERE
clients.c_id=' + sID )

# Get first query result
oResult1 = oCursor.fetchone()

# Define new rules
sFrom_rule_orig = oResult1[0]
sFrom_rule = sFrom_rule_orig.replace( '$id', sID )
sWhere_rule = str( oResult1[1] )
sOrder_rule = str( oResult1[2] )

# Execute second query
oCursor.execute( ' SELECT jobs.job_id, jobs.type_id, tasks.frame,
jobs.status_id ' + sFrom_rule + sWhere_rule + sOrder_rule + ' LIMIT 1 ' )

This prints

# Get result for second query, not sure if I'm doing the right thing
oResult2 = oCursor.fetchone()

>>> print oResult2

I'm probably doing something wrong here, but with the activeX objects it
would have printed something like
job_id=integer, type_id=integer, frame=integer, status=integer


More information about the Tutor mailing list