Special characters in SQLite ® etc

Discussion in 'iOS Programming' started by MontyClift, Oct 7, 2008.

  1. MontyClift macrumors newbie

    Joined:
    Sep 2, 2008
    #1
    Hi All,

    Is there a way to use symbols like ® in SQLite and the iPhone SDK?
    If I do a .read "myfile.sql" that imports data to a table and some of that data contains the ® symbol it goes in fine. However when querying in Terminal>SQLite it shows up as ?
    And then in the NSString I create from the field I get no value
    Anyone any ideas?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    sqlite uses utf-8 so make sure your strings going into it are in that encoding and that when you get a string from sqlite you create it with that encoding.
     
  3. MontyClift thread starter macrumors newbie

    Joined:
    Sep 2, 2008
    #3
    When I retrieve the value from the database I am creating it with
    Code:
    [NSString stringWithUTF8String]
    so I presume that should take care of the extraction side of it?
    When I am inserting it to the database it is coming from a .sql file for example with
    Code:
    Insert into MyTable(Field1)values('a');
    Insert into MyTable(Field1)values('b');
    Insert into MyTable(Field1)values('®');
    
    The only values I am having issues with are where I use ®
    Are you saying the encoding of the .sql file itself may be wrong? Is there a way to check/change that? Sorry if thats a stupid question but never done this before.

    I could just use (R) or do a string replace in the objective c code to get around it but I'd like to know how to do this properly.

    Thanks
     
  4. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #4
    Yes, it's the encoding of the text in the sql file that's the problem. It's apparently not UTF8. Since most Mac text editors default to MacRoman, that's probably what it is.

    If the text file was created with something other than a text editor then it might be some other encoding and you'll have to test it.
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    The Xcode editor supports different encodings. I also use TextWrangler, which is arguably a little easier for this. Open the file in one of those editors. In Xcode open a Get Info window for the file (you might need to add it to a project). In the info window it tells you what encoding it thinks the file is and you can change the encoding to something else or tell it to reinterpret the file as another encoding.

    TextWrangler shows what encoding it thinks the file is at the bottom of the window. There's a menu item 'ReOpen using encoding' which will make the app reinterpret the file as another encoding.

    You need the file to be in utf-8.
     
  6. MontyClift thread starter macrumors newbie

    Joined:
    Sep 2, 2008
    #6
    Thanks for the help folks, got it working by saving the files as utf-8
     

Share This Page