How do I quote a UTF-8 String Literal in Sqlite3

前端 未结 4 1232
北海茫月
北海茫月 2020-12-20 20:37

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 some

相关标签:
4条回答
  • 2020-12-20 21:13

    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);
    é
    

    Edit: Since this answer was originally written, a CHAR function has been added to SQLite. So now, you could write

    INSERT INTO MyTable(MyColumn) VALUES(CHAR(233))
    
    0 讨论(0)
  • 2020-12-20 21:31

    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                                                                    
    0 讨论(0)
  • 2020-12-20 21:32

    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.

    0 讨论(0)
  • 2020-12-20 21:33

    If your problem is reinterpretation of escape sequences in sqlite you can (ab)use json_extract eg.

        UPDATE  `tableToFix`  SET  `columnToFix` = json_extract('"'  ||  `columnToFix`  ||  '"', '$');
        INSERT INTO test VALUE (json_extract('"P\u0159\u00edli\u0161 \u017elu\u0165ou\u010dk\u00fd k\u016f\u0148 \u00fap\u011bl \u010f\u00e1belsk\u00e9 \u00f3dy."', '$'));
    

    Notice: quotes handling. Valid json string starts and ends with " so you must add them before use of json_extract

    0 讨论(0)
提交回复
热议问题