gpt4 book ai didi

java - 从sqlite数据库android获取所有表名列表到数组中

转载 作者:行者123 更新时间:2023-11-29 03:13:38 27 4
gpt4 key购买 nike

如何将我在 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();
}

最佳答案

试试这个代码示例

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

关于java - 从sqlite数据库android获取所有表名列表到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27884310/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com