Question

I build in sqlite3 3 tables. When I'm doing the following SELECT command:

select us.saleSpecificProduct,us.fAtMall,u.name,u.gender,st.storeName
  from UsersSale us,Users u,Stores st
 where u.userID=us.userID and st.storeID=us.saleStoreID order by us.saleID

It work ok in the shell, but if i put this statement in my iphone application I'm getting an error.

/*
 * read items from the database and store in itemw
 *
 */
-(void)readItems {

if (!database) return; // earlier problems

// build select statement
if (!selStmt)
{
  const char *sql =  "select us.saleSpecificProduct,us.fAtMall,u.name,u.gender,st.storeName from UsersSale us,Users u,Stores st where u.userID=us.userID and st.storeID=us.saleStoreID order by us.saleID"; 
      if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK)
    {
        selStmt = nil;
    }

When I execute the application I get error in "sqlite3_prepare_v2" }

What is wrong?

Was it helpful?

Solution

Maybe try putting your parameters inside single quotes

So something like:

WHERE u.userID = 'us.userID' AND st.storeID = 'us.saleStoreID' ORDER BY us.saleID

Also you should make use of bindings to get safe statements:

e.g.

NSString *strGet = @" . . . WHERE u.userID = ? AND st.storeID = ? ORDER BY us.saleID"

if(sqlite_prepare_v2(...) == SQLITE_OK)
{
    sqlite3_bind_int(stmtGet, 1, us.userID);
    sqlite3_bind_int(stmtGet, 2, us.saleStoreID);

    while(sqlite3_step(stmtGet) == SQLITE_ROW)
    {
        DatabaseDataObject *myDataObj = [[DatabaseDataObject alloc] init];

        myDataObj.objID = sqlite3_column_int(0);
        myDataObj.postcode = sqlite3_column_int(1);

        // add to parameter array of objects
        [myArr addObject:myDataObj];

        [myDataObj release];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top