Java 将所有表名列表从sqlite数据库获取到数组中

Java 将所有表名列表从sqlite数据库获取到数组中,java,android,sqlite,android-sqlite,Java,Android,Sqlite,Android Sqlite,如何将我在SQlite数据库中创建的所有表转换为字符串数组?请给我建议。提前感谢我的数据库课程,如下所示 private static class DBHelper extends SQLiteOpenHelper { /* public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, facto

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

我用这样的代码解决问题

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

除了在方法中返回列表之外,它与我的答案有何不同?您的答案是write,但我无法理解您答案中的这一行SqlHelper SqlHelper=new SqlHelper this,TK.db,null,1,谢谢你的重播。这两个代码都成功解决了我的问题