Pergunta

i am using following code but sqlite giving me error

 NSArray* a = [NSArray arrayWithObjects:
              @"\""
              @"\\ \"",
              nil];
quantity=[quantity stringByReplacingOccurrencesOfString:[a objectAtIndex:0 ] withString:[a objectAtIndex:1]];
queryString = [NSString stringWithFormat:@"INSERT INTO SHOPINGLIST (QUANTITY) VALUES (\"%@\")",quantity];

it gives error that:- near "diax1": syntax error in query INSERT INTO SHOPINGLIST original quantity is =1 slice,medium(4-1/2"diax1/8"thick)

Foi útil?

Solução

To insert special characters in SQLite in iOS, use placeholders like (also please check which special characters are allowed in Sqlite):

queryString = @"INSERT INTO SHOPINGLIST (QUANTITY) VALUES (?)";
sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, queryString, -1, &compiledStatement, NULL) == SQLITE_OK) {
        sqlite3_bind_text(compiledStatement, 1, [quantity UTF8String], -1, SQLITE_TRANSIENT);
  // Use sqlite3_bind_text for string values. For other type of values, check sqlite documentation.
 // .... And So on.

    }

Outras dicas

for double value, use this:-

sqlite3_bind_double(compiledStatement, 1, [quantity UTF8String], -1, SQLITE_TRANSIENT);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top