I'm trying to create arabic database in android application and want to see how I can achieve that?
I know that I will have to do encoding and stuff. But I want to know if someone really tried to create non-english database and help me to do the best approach.
So for create SQLite
best approach is use SQLiteOpenHelper
and create your own class that extending from SQLiteOpenHelper
, implements onCreate()
, method and onUpgrade()
method.
Example how to create database:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "basic_login_system_database";
protected static final String USER_TABLE_NAME = "User";
protected static final int DATABASE_VERSION = 1;
protected static final String KEY_ID = "id";
protected static final String KEY_NAME = "name";
protected static final String KEY_SURNAME = "surname";
protected static final String KEY_PASSWORD = "password";
protected static final String KEY_EMAIL = "email";
protected static final String KEY_ADDRESS = "adress";
protected static final String KEY_AGE = "age";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_USER_TABLE = "CREATE TABLE " + USER_TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_SURNAME + " TEXT NOT NULL, "
+ KEY_PASSWORD + " TEXT NOT NULL, "
+ KEY_EMAIL + " TEXT UNIQUE NOT NULL, "
+ KEY_ADDRESS + " TEXT, "
+ KEY_AGE + " TEXT" + ")";
db.execSQL(CREATE_USER_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String DELETE_USER_TABLE = "DROP TABLE IF EXISTS " + USER_TABLE_NAME;
db.execSQL(DELETE_USER_TABLE);
onCreate(db);
}
}
And you must set encoding for Arabic
characters because implicitly is it set on utf-8
.
private static final String ENCODING_SETTING = "PRAGMA encoding ='windows-1256'";
And you set it in your onOpen()
method.
@Override
public void onOpen(SQLiteDatabase db) {
if (!db.isReadOnly()) {
db.execSQL(ENCODING_SETTING);
}
}
Then for retrieve data from your database just use Cursor
with methods rawQuery()
or query()
.
Example: Cursor c = db.raQuery(SELECT_STATEMENT, null);
For insert data use insert()
method of SQLiteDatabase
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With