简体   繁体   English

SQLiteAssetHelper - “没有这样的表”错误

[英]SQLiteAssetHelper - 'No such table' error

I have prepopulated a database with DB Browser for SQLite and tried to retrieve data from it using SQLiteAssetHelper (installed and used it using the guide at https://github.com/jgilfelt/android-sqlite-asset-helper ) and got a "No such table" error.我已经使用 DB Browser for SQLite 预先填充了一个数据库,并尝试使用 SQLiteAssetHelper 从中检索数据(使用https://github.com/jgilfelt/android-sqlite-asset-helper 上的指南安装和使用它)并获得了“没有这样的表”错误。 I am sure that there is a table named that way in the database.我确信数据库中有一个以这种方式命名的表。 I've tried to debug it and saw that I leave the DATABASE_VERSION as equal to one than it detects that there is already a database and doesn't copy it, though if I change the database version and setForceUpdate to (true) then it copies it, but I get the same error anyway.我试图调试它并看到我将 DATABASE_VERSION 保留为等于 1,而不是它检测到已经有一个数据库并且不复制它,但是如果我将数据库版本和 setForceUpdate 更改为 (true) 然后它会复制它,但无论如何我都会遇到同样的错误。 What should I do for SQLiteAssetHelper to copy my database properly and get rid of that annoying error?我应该怎么做才能让 SQLiteAssetHelper 正确复制我的数据库并摆脱那个烦人的错误?

Code:代码:

DataBaseHelper class:数据库助手类:

public class DataBaseHelper extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "DonRoll";
private static final int DATABASE_VERSION = 2;

private static DataBaseHelper instance;

public static DataBaseHelper getInstance(Context context){
    if (null == instance){
        instance = new DataBaseHelper(context);
    }
    return instance;
}

private DataBaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public Cursor getAllCategoryNames() {
    setForcedUpgrade(2);
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables("MenuCategories");
    Cursor c = qb.query(db, null, null, null, null, null, null);
    c.moveToFirst();
    return c;
}

} }

Activity class:活动类:

public class CategoryListActivity extends ListActivity {

DataBaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category_list);
    db = DataBaseHelper.getInstance(this);
    getData();
}

@SuppressWarnings("deprecation")
private void getData() {
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            db.getAllCategoryNames(),
            new String[] { "value" },
            new int[] { android.R.id.text1 });

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(adapter);
}

} }

Problem solved: I've used adb pull to get the database file from the assets folder and it somehow got corrupted - it had only the "android_metadata" table.问题已解决:我使用adb pull从资产文件夹中获取数据库文件,但它以某种方式损坏了 - 它只有“android_metadata”表。 I don't know why this happened (maybe because I renamed it after I have put it into Android studio?), but once I have deleted this file and replaced it with a proper one everything worked just fine.我不知道为什么会发生这种情况(也许是因为我在将它放入 Android Studio 后重命名了它?),但是一旦我删除了这个文件并将其替换为一个合适的文件,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM