Dynamic SELECT statement



  • I would like to perform a query where I am able to select various records based on a number of where statements that could be different each time.

    So in a table that could be like this:

    ID - integer
    Name - varchar
    Description - varchar
    Date - datetime
    Value - integer
    Deleted - bool
    

    What I want to do is have a sort of filtering ability where I select values based on a number of user defined conditions. This means that at every call some of the variables might be null because they are not of interest.

    What would be a good way of achieving this?


  • Lifetime Qt Champion

    Hi,

    Sounds like a job for a query builder. Each time you modify one of these variable, you rebuild the query using condition based on what you need. i.e.:

    QString query = "SELECT * FROM ";
    if (condition) {
        query += "WHERE foo=\"" + parameterValue + "\"";
        //etc.
    }
    

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.