我正在做一个项目,我有一个包含 id、标题、内容的 sqlite 数据库能够从数据库中读取内容。希望我的问题很清楚!谢谢。
这是我的 DatabaseHelper 类
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "alQais.db";
private Context context;
private SQLiteDatabase sqLiteDatabase;
public DatabaseHelper(Context mContext){
super(mContext, DBNAME, null, 1);
this.context = mContext;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDatabase(){
String dbPath = context.getDatabasePath(DBNAME).getPath();
if(sqLiteDatabase != null && sqLiteDatabase.isOpen()){
return;
}
sqLiteDatabase = SQLiteDatabase.openDatabase(dbPath, null,SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase(){
if(sqLiteDatabase != null){
sqLiteDatabase.close();
}
}
public ArrayList getAllTitles(){
ArrayList arrayList = new ArrayList();
openDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from muallaqat", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()){
arrayList.add(cursor.getString(cursor.getColumnIndex("title")));
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return arrayList;
}
public String getMuallaqa(String title){
String muallaqat;
openDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from muallaqat where title like '" + title + "'", null);
cursor.moveToFirst();
muallaqat = cursor.getString(cursor.getColumnIndex("muallaqa"));
cursor.close();
closeDatabase();
return muallaqat;
}}
慕森卡
12345678_0001
素胚勾勒不出你
相关分类