Thanks RSmith.

It works.

But, I am looking for single query for prepared statements. That's the
actual struggle for me.

Ok, but you give code examples that has nothing to do with prepared statements.

Giving this one last push, I iwll try to ignore all you have said and simply show the best way to do it with prepared statements (much like Igor noted) in some pseudo-code trying to avoid any ambiguity - Hope this is easily understood.


function - bool deleteEmps( key_param, name_param ) {

    string sQuery = "DELETE FROM `Emp` WHERE (key = ?1) AND (( ?2 = '' ) OR ( ?2 = 
name )); "

    pointer stmt;
    int sqlResult = sqlite3_prepareV2(dbHandle, sQuery, sQuery.length, stmt, 
null );

    while ( sqlResult == SQLITE_OK || sqlResult == SQLITE_ROW )
    {
        if ( sqlite3_bind_int(stmt, 1, key_param) == SQLITE_OK  )
        if ( sqlite3_bind_text(stmt, 2, name_param == SQLITE_OK )
        {
            sqlResult = sqlite3_step(stmt);
        }
    }

    return (sqlResult == SQLITE_DONE);
}


Notes:
No need to check if the second parameter (?2) is NULL in this case, because I forcibly bind it every iteration, it can only be == name_param - which in turn can be empty, but cannot be regarded as NULL.

Most of the bracketing and spaces are superfluous but harmless, and only added 
for clarity.

In the bindings, I simply KNOW that I am dealing with parameters 1 and 2 because I specified them just so in the Query, but this should really be automated or using named parameters (:key, :name etc) with binding on names or index-of-name kind of resolves. If this procedure only ever does this one thing, it matters none - but that would be rather inefficient.

I assumed the code is obvious, but in case you are not familiar:
&& means logical AND
|| means logical OR.
== means testing equality
= means assignment of a value.

There are no fault reporting, typically if the return statement is reached and it is NOT SQLITE_DONE, it means something went wrong. The prepare and binds should all return SQLILTE_OK and the step may return SQLITE_ROW or SQLITE_DONE depending on whether it is finished or there are more work to be done. Of course, ANY of them can return a different value which would mean an immediate error occured.

This must definitely do what you require - if it doesn't, please post your 
actual code.




_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to