java – 插入并选择阿拉伯数据Android SQLite

前端之家收集整理的这篇文章主要介绍了java – 插入并选择阿拉伯数据Android SQLite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试在Android应用程序中创建阿拉伯数据库,并希望看到我如何实现这一目标?

我知道我将不得不做编码和东西.但我想知道是否有人真的试图创建非英语数据库并帮助我做最好的方法.

最佳答案
因此,对于创建sqlite,最好的方法是使用sqliteOpenHelper并创建自己的类,从sqliteOpenHelper扩展,实现onCreate(),方法和onUpgrade()方法.

示例如何创建数据库

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

并且您必须为阿拉伯字符设置编码,因为它隐含在utf-8上设置.

private static final String ENCODING_SETTING =“PRAGMA encoding =’windows-1256’”;

然后在onOpen()方法中设置它.

@Override
public void onOpen(sqliteDatabase db) {
   if (!db.isReadOnly()) {
      db.execsql(ENCODING_SETTING);
   }
} 

然后,要从数据库中检索数据,只需使用Cursor with methods rawQuery()或query().

示例:Cursor c = db.raQuery(SELECT_STATEMENT,null);

对于插入数据,请使用sqliteDatabase的insert()方法.

原文链接:https://www.f2er.com/android/430632.html

猜你在找的Android相关文章