Exec statement problem

D'Arcy J.M. Cain darcy at vex.net
Sat Oct 9 06:52:13 EDT 1999


maxsbox <maxsbox at scds.co.za> wrote:
> I have been trying to pass a variable to a query statement.

> The statement exec: appears to be the answer, however I cannot get it to
> work

> wrote the following:

> def art2(db, z):
>     y = 'db.query("SELECT * FROM artisan WHERE name = \''
>     y = y+z
>     y = y+ '\'")'
>     print y    # prints correct string
>     exec y    # gives fault message

> db is the connected data base
> z is the required variable, in this case 'Nico"
> Also tried a few variations of the exec statement with the same result

Why exec?  Just build your query statement and pass it to the query
method directly.

def art2(db, z):
    query = """SELECT * FROM artisan WHERE name = '%s'""" % z
	print query
	db.query(query)

Or this.

def art2(db, z):
	db.query("""SELECT * FROM artisan WHERE name = '%s'""" % z)

You can get quite creative with your queries.  I certainly do.

-- 
D'Arcy J.M. Cain <darcy at caingang.com>      |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on         
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.




More information about the Python-list mailing list