gpt4 book ai didi

android - 如何从 SQLite 数据库 Android Studio 中获取所有表

转载 作者:行者123 更新时间:2023-12-05 00:18:24 25 4
gpt4 key购买 nike

我一直在寻找如何从数据库中获取所有表,我不熟悉 SQLite for Android Studio。我有一个 DBHelper 类,我用它来制作我的 sql 命令。到目前为止,我收集到的是我必须对 sqlite_master 进行查询,例如“ SELECT name FROM sqlite_master WHERE type='table' ”。
这是我的 DBHelper 类:

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;
}
我只粘贴到 GetDeckList 方法的位置。
这是我引用 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);
}
我想我错过了一些代码,因为我正在做我在 stackoverflow 上阅读的关于如何从 sqlite_master 查询的内容,但是我的应用程序不断崩溃,因为我一直在尝试创建一个“空”表。当我修改代码时,它一直在调用“ onCreate ”或“ onOpen ”,即使我没有尝试制作表格。所以它一直在崩溃,因为我没有给它一个新表。所以我假设我错过了一些代码,但我不知道它是什么。

最佳答案

试试这样

db = new DBHelper(this);
Cursor cData = db.GetDeckList();
if(cData != null) {
int numOfRows = cData.getCount();
if(numOfRows == 0){
Toast.makeText(this, "Deck list is empty!", Toast.LENGTH_SHORT).show();
}else{

List<String> tableNames = new ArrayList<String>();
cData.moveToFirst();
while (!cData.isAfterLast()) {
tableNames.add(cData.getString(cData.getColumnIndex("name")));
cData.moveToNext();
}
mAdapter = new RecycleViewAdapter(true, tableNames, this);

layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(false);
}
}

关于android - 如何从 SQLite 数据库 Android Studio 中获取所有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68218964/

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