Application crashing due to unicode characters in string

I am making an application in which I have to store data from remote db into local sqlite3 db.

Problem is - when any string to be inserted contains unicode character such as: \U00a0 or \U2022 or \U2019, it crashes.

The part of code which crashes is this-

NSString *insertQuery = @"insert into XYZ(field1,field2) values (@"1",[recordDict objectForKey:@"field2"]);

// recordDict contains value: @"\U00a0 \U00a0 \U00a0 \U00a0The Cadfsdfsdfptain\U2019s" for key: @"field2"

sqlite3_stmt *insertStmnt;
const char *sql = [insertQuery cStringUsingEncoding:1];

printf("insertQuery - %s",sql); // it is showing insertQuery - (null) and crashing at next line
if(sqlite3_prepare_v2(database, sql, -1, &insertStmnt, NULL) != SQLITE_OK){
    NSLog(@"Error while creating insert statement. '%s'",sqlite3_errmsg(database));
}

Can anyone suggest me how to resolve this problem?

Thanks,

Miraaj

Answers


cStringUsingEncoding: returns NULL when the string cannot be losslessly converted to the specified encoding, hence your crash. You can use canBeConvertedToEncoding: to ensure the conversion is possible.

Furthermore, the value 1 corresponds to NSASCIIStringEncoding which will certainly fail for a string with Unicode characters. NSUTF8StringEncoding might be more appropriate.


Need Your Help

Excel Macro to group

excel vba excel-vba group

Hi I'm not sure if a macro would be the best solution to this problem, but I am looking to group rows and put in an if statement to make it a little easier.

How to enable access control in SQLite?

c sqlite encryption sqlite3 access-control

We have an application which uses SQLite as an embedded RDBMS. Initially the application was meant for a single machine and single user. But with time the scope of the project has expanded. Now one...