Application crashing due to unicode characters in string

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

  1. macrumors regular

    Joined:
    May 24, 2009
    #1
    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-

    Code:
    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,

    Monaj
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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.

    -Lee
     

Share This Page