#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0

    Dynamically Building a Select Statement


    Hi All,

    I'm creating a search form with different fields that you can search for (i.e. FirstName, LastName, Country, etc). Anybody have an idea on how I can create a dynamic select statement that can include all of the fields or just 1 of the fields in the search query depending on what field the user fills out?

    Any help would be greatly appreciated. Thanks!
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    As you describe it, it sounds to me like a javascript problem (being a client side action).

    grimey
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    i guess javascript could be used as well to build the Select statement to query the database but i was hoping someone can help me out using a python solution because that's what i need to use.

    basically the select statement will look something like this:

    (if FirstName, LastName, Country where filled out)
    select * from table where FirstName like "blah" and where LastName like "blah" and where Country like "blah"

    (if only Firstname was filled out)
    select * from table where FirstName like "blah"

    i guess you get the idea...any thoughts?
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Ah,
    The data from a form is used to construct an SQL query.
    You need the appropriate module (something like mysqldb). Here is a tutorial that walks you through it:
    http://www.kitebird.com/articles/pydbapi.html

    grim
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    We also have a nice article that describes how to use MySQLdb in Devsheds Python section:

    http://www.devshed.com/c/a/Python/MySQL-Connectivity-With-Python/

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    Thanks guys. I'll try it out.

IMN logo majestic logo threadwatch logo seochat tools logo