简体   繁体   中英

Special Characters JSON iOS SQLite

I have one question. I'm getting information from JSON and he returns me invalid character. JSON give me this: "27/"" when I need this: 27". I understand this is a encode for special characters but when I use the value on NSString to make an Insert in a SQLite table, I can't use 27/" cause the insert format is this: INSERT INTO FORMATOS (ID, NOMBRE) VALUES ("17", "27"").

What method I need to Insert information correctly in the SQLlite?

for (int i = 0; i<idFormato.count; i++) {
            NSString *idStr = [idFormato objectAtIndex:i];
            NSString *nameStr = [nameFormato objectAtIndex:i];

            insertSQL = [NSString stringWithFormat:@"INSERT INTO FORMATOS (ID, NOMBRE) VALUES (\"%@\", \"%@\")", idStr, nameStr];

            //Char constant with the query encoded un UTF
            const char *insert_stmt = [insertSQL UTF8String];
            //Execute query
            sqlite3_prepare_v2(dieneDB, insert_stmt, -1, &statement, NULL);

            //Check if Statment is dne correctly
            if(sqlite3_step(statement) == SQLITE_DONE){
                NSLog(@"Guardado Formatos correctamente");
            }

The JSON:

[

    {"ID_FORMATO_INT":"17","NOMBRE_FORMATO_STR":"2,5\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:17:55","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"18","NOMBRE_FORMATO_STR":"4\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:18:20","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"19","NOMBRE_FORMATO_STR":"4,7\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:20:07","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"20","NOMBRE_FORMATO_STR":"5,5\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:20:15","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"21","NOMBRE_FORMATO_STR":"9,7\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:20:42","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"22","NOMBRE_FORMATO_STR":"7,9\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:21:04","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"23","NOMBRE_FORMATO_STR":"11\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:22:40","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"24","NOMBRE_FORMATO_STR":"13\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:22:44","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"25","NOMBRE_FORMATO_STR":"15\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:22:49","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"26","NOMBRE_FORMATO_STR":"21,5\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:23:11","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null},{"ID_FORMATO_INT":"27","NOMBRE_FORMATO_STR":"27\"","ID_USUARIO_ALTA_INT":"3","FECHA_ALTA_FORMATO_DAT":"2014-09-18 07:23:14","ID_USUARIO_MOD_INT":null,"FECHA_MOD_FORMATO_DAT":null}

]

The issue is that you're building your SQL with stringWithFormat . That is a practice that is susceptible to this sort of problem. Instead, use ? placeholders in your SQL and then use sqlite3_bind_text to bind values to the ? placeholders. See the sqlite3_bind_text() help for more information.

For example, you might:

const char *insert_stmt = "INSERT INTO FORMATOS (ID, NOMBRE) VALUES (?, ?)";

if (sqlite3_prepare_v2(dieneDB, insert_stmt, -1, &statement, NULL) != SQLITE_OK) {  // prepare SQL
    NSLog(@"prepare error: %s", sqlite3_errmsg(dieneDB));
} else {
    if (sqlite3_bind_text(statement, 1, idStr, -1, NULL) != SQLITE_OK) {            // bind 1
        NSLog(@"bind idStr error: %s", sqlite3_errmsg(dieneDB));
    } else if (sqlite3_bind_text(statement, 2, nameStr, -1, NULL) != SQLITE_OK) {   // bind 2
        NSLog(@"bind nameStr error: %s", sqlite3_errmsg(dieneDB));
    } else if (sqlite3_step(statement) != SQLITE_DONE) {                            // perform SQL
        NSLog(@"step error: %s", sqlite3_errmsg(dieneDB));
    } else {
        NSLog(@"Guardado Formatos correctamente");
    }

    sqlite3_finalize(statement);
}

I just typed this in, so please forgive any typos, but hopefully it illustrates the idea.

Note, I also (a) check all of these return codes; (b) log the error if any; and (c) finalize the statement when done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM