最佳答案
因此,对于创建sqlite,最好的方法是使用sqliteOpenHelper并创建自己的类,从sqliteOpenHelper扩展,实现onCreate(),方法和onUpgrade()方法.
原文链接:https://www.f2er.com/android/430632.html示例如何创建数据库:
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);