Pregunta

Construyo en SQLite3 3 tablas. Cuando estoy haciendo el siguiente comando seleccionar:

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

Funciona bien en el shell, pero si pongo esta declaración en la aplicación de mi iPhone, recibo un 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;
    }

Cuando ejecuto la aplicación recibo error en "SQLITE3_PREPARE_V2"}

¿Qué está mal?

¿Fue útil?

Solución

Tal vez intente poner sus parámetros dentro de citas individuales

Entonces algo como:

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

También debe hacer uso de enlaces para obtener declaraciones seguras:

p.ej

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];
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top