如何获取所有表是我在SQlite数据库中创建的字符串数组?。请给我建议。在此先感谢我的数据库类如下
private static class DBHelper extends SQLiteOpenHelper {
/* public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}*/
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VIRSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+DB_TABLE+" ("+
KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
KEY_NAME+" TEXT NOT NULL, "+
KEY_MOBILE+" NUMBER NOT NULL, "+
KEY_DATE+" TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+DB_TABLE);
}
}
public MyDatabase(Context c){
ourContext=c;
}
public MyDatabase open(){
ourHelper=new DBHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
答案 0 :(得分:4)
试试此代码示例
ArrayList<String> arrTblNames = new ArrayList<String>();
SqlHelper sqlHelper = new SqlHelper(this, "TK.db", null, 1);
SQLiteDatabase DB = sqlHelper.getWritableDatabase();
Cursor c = DB.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
if (c.moveToFirst()) {
while ( !c.isAfterLast() ) {
arrTblNames.add( c.getString( c.getColumnIndex("name")) );
c.moveToNext();
}
}
答案 1 :(得分:0)
我像这段代码一样解决问题
public ArrayList<String> getAllTable() {
ArrayList<String> arrTblNames = new ArrayList<String>();
Cursor c = ourDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
c.moveToFirst();
while (!c.isAfterLast()) {
arrTblNames.add(c.getString(c.getColumnIndex("name")));
c.moveToNext();
}
// make sure to close the cursor
c.close();
return arrTblNames;
}