[DB-SIG] Mysqldb python variable in request
Vernon Cole
wnvcole at peppermillcas.com
Thu Jan 22 11:47:39 EST 2004
There is some confusion here between supplying parameters to the SQL server,
which is done differently between different servers, and a python string
substitution.
The python method always works, but is less efficient for repetative
searches.
This snippet from one of my programs does something like you want:
Sel = "select AuditDate from BB_PeriodEnd where (Casino_Code =
'%s' and Period_ID = 5 and AuditDate = '%s')" % (Casino_Code, AuditDate)
pdEnd.execute(Sel)
Note that all substitution is done in python, so I can check the syntax
using a python print statement. Also note that there are single quotes
around the %s constructs. These single quotes are passed to the SQL server,
which needs to see a string literal at that point. Python is filling in the
value for the literal. In your case, try something like:
cursor.execute(""" select %s from Xml2Mysql where %s='%s' """ % (target,
constraint, constraint_value))
-----Original Message-----
From: samira M'Bata [mailto:bouataa at hotmail.com]
Sent: Thursday, January 22, 2004 2:38 AM
To: db-sig at python.org
Subject: [DB-SIG] Mysqldb python variable in request
Hi!
i hope someone could help me.
I d like to make an sql statement with python, in which the column names
will be parameter of a python function.
Bevore I tried to define my definition, i tried this code:
target= 'DataType'
constraint= 'Location'
#DataType and Location are two columns of my Table
constraint_value= 'stuttgart'
cursor.execute(""" select %s from Xml2Mysql where %s=%s """,(target,
constraint, constraint_value))
I think the problem comes from the fact that my columns names are not as a
string type defined.
Should I change something in my table definition?
_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
_______________________________________________
DB-SIG maillist - DB-SIG at python.org
http://mail.python.org/mailman/listinfo/db-sig
More information about the DB-SIG
mailing list