views:

136

answers:

3

I'm looking to encode and store Unicode in a Sqlite database. Is there any way to raw encode a UTF-8 (unicode) string literal in a sql query.

I'm looking for something similar to java where I can toss a \u00E9 into a string and have it automagically upconvert to Unicode.

A: 

If you configure your database to use UTF-8 (I believe this is default for many installations; do PRAGMA encoding="UTF-8"; at schema creation time to be certain), this shouldn't be an issue.

If you send SQLite3 a set of characters encoded in UTF-8, it should have no problem dealing with it.

If Java has the ability to allow you to "toss a \u0039 into a string", I'd just use that, and ensure that when you try to place the string into the database, that you have the string convert to a UTF-8 byte encoding using whatever mechanism Java provides. I do not believe SQLite provides or needs to provide this for you.

sheepsimulator
Why would you "toss a \u0039 into a string"? Seems it'd be a whole lot easier just to write "9".
dan04
+2  A: 

What language are you using? SQLite handles Unicode just fine, creating the literals in your hosting language is less obvious.

$ sqlite3 junk.sqlite
SQLite version 3.6.22
sqlite> create table names (id integer primary key, name string);
sqlite> insert into names values (null, 
    'î℉ yõù gѷЄ ΣϘГくטƏ UTF-8, it stores it');
sqlite> select * from names;
1|î℉ yõù gѷЄ ΣϘГくטƏ UTF-8, it stores it
msw
Nice test string.
chryss
+1  A: 

SQLite doesn't have escape sequences. But your programming language probably does.

# in Python
db.execute("INSERT INTO MyTable(MyColumn) VALUES('\u00E9')")

or

db.execute("INSERT INTO MyTable(MyColumn) VALUES(?)", ['\u00E9'])

If for some reason you have to write a UTF-8 literal in pure SQL, you can do something like:

sqlite> SELECT CAST(X'C3A9' AS TEXT);
é
dan04