Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure the SQLite on Android uses UTF-8 as a charset

Tags:

android

sqlite

I am developing an Android application using SQLite as backend.

I want to make sure all the tables in the database use UTF-8. How can I achieve that?

I have tried:

CREATE TABLE myTable (_all_columns_definitions_) DEFAULT CHARSET=utf8;

but a syntax error arose.

like image 367
Dan Avatar asked Nov 25 '10 10:11

Dan


People also ask

What encoding does SQLite use?

The default encoding will be UTF-8 for databases created using sqlite3_open() or sqlite3_open_v2(). The default encoding for databases created using sqlite3_open16() will be UTF-16 in the native byte order.

Can you use SQLite on Android?

The Android SDK includes a sqlite3 shell tool that allows you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases.

What data type does SQLite support Android?

Date and Time Datatype SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values: TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS. SSS").


3 Answers

Given that sqlite only supports UTF-8 and UTF-16 as the encodings, you would have noticed if Android would create databases in something other than UTF-8. sqlite3_open defaults to create the database in UTF-8, and that is what Android is likely to use.

like image 186
Martin v. Löwis Avatar answered Oct 03 '22 13:10

Martin v. Löwis


You need to make use of the encoding PRAGMA:

PRAGMA encoding = "UTF-8";
like image 22
Alix Axel Avatar answered Oct 03 '22 15:10

Alix Axel


By default Android SQLite uses UTF-8. I had the same problem with special characters, but because when I populated the database on the first launch I used a txt file with another charset.

like image 2
Marco C Avatar answered Oct 03 '22 15:10

Marco C