Application crashing due to unicode characters in string

Discussion in 'Mac Programming' started by Monaj, Dec 1, 2010.

  1. Monaj macrumors regular

    May 24, 2009
    Hi all,

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

    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?


  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    OK, before we start don't use 1 as the parameter to cStringUsingEncoding:. Use one of the named constants instead.

    You can check if a string can be converted to a C-String in a particular encoding using canBeConvertedToEncoding:. If this returns NO you can only turn this into a C-String using a lossy conversion or using a different encoding. The encoding you have specified is NSASCIIStringEncoding which is described in the documentation as:

    "Strict 7-bit ASCII encoding within 8-bit chars; ASCII values 0…127 only."

    Clearly you cannot ever expect your Unicode characters to be representable in this encoding.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    On top of what robbieduncan said, the first line doesn't even compile for me. it seems like you trying to use a sort of perl-ism in pulling variables into a string literal, or something like that.


Share This Page