I've been searching on how to get all tables from a database, I'm not familiar with SQLite for Android Studio. I have a DBHelper class that I use to make my sql commands. What I've gathered so far is that I have to make a query to sqlite_master such as "SELECT name FROM sqlite_master WHERE type='table'".
Here is my DBHelper class:
public class DBHelper extends SQLiteOpenHelper
{
public String tableName;
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_DESC = "desc";
private static final String COLUMN_DECKNAME = "deckname";
private static final String COLUMN_IMAGENAME = "imagename";
public DBHelper(Context context, String tablename) {
super(context, "UserDecks.db", null, 1);
tableName = tablename;
}
public DBHelper(Context context){
super(context, "UserDecks.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + tableName + "(" + COLUMN_NAME + " TEXT, " + COLUMN_DESC + " TEXT, " + COLUMN_DECKNAME + " TEXT, " + COLUMN_IMAGENAME + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + tableName);
}
@Override
public void onOpen(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + "(" + COLUMN_NAME + " TEXT, " + COLUMN_DESC + " TEXT, " + COLUMN_DECKNAME + " TEXT, " + COLUMN_IMAGENAME + " TEXT)");
}
public Cursor GetDeckList(){
SQLiteDatabase sdb = this.getReadableDatabase();
Cursor cursor = sdb.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
return cursor;
}
I only pasted where it reaches up to the GetDeckList method.
This is where I reference DBHelper:
db = new DBHelper(this);
Cursor cData = db.GetDeckList();
int numOfRows = cData.getCount();
if(numOfRows == 0){
Toast.makeText(this, "Deck list is empty!", Toast.LENGTH_SHORT).show();
}else{
while(cData.moveToNext()){
DL_deckList = new DeckList(cData.getString(0));
LIST_currentDecks.add(DL_deckList);
}
mAdapter = new RecycleViewAdapter(true, LIST_currentDecks, this);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(false);
}
I think I am missing some piece of code, because I'm doing what I've read on stackoverflow on how to query from sqlite_master, but my app keeps crashing because I keep trying to create a "null" table. It keeps calling "onCreate" or "onOpen" when I tinker with the code, even though I'm not trying to make a table. So it keeps crashing because I'm not giving it a new table. So I'm assuming I'm missing some code, but I don't know what it is.