简体   繁体   English

BaseAdapter中的SQLiteDatabase.openDatabase失败,但是openOrCreateDatabase在Activity中工作正常

[英]SQLiteDatabase.openDatabase in BaseAdapter fails but openOrCreateDatabase works fine in Activity

I create my database in the onCreate method of an Activity. 我在Activity的onCreate方法中创建数据库。 In this Activity I can add records and I can query records. 在此活动中,我可以添加记录,也可以查询记录。 If there are records that result from my query I add them to a custom ListView, hence the BaseAdapter class. 如果查询产生了记录,则将它们添加到自定义ListView中,从而添加到BaseAdapter类中。 The ListView contains two TextViews and a Delete Button. ListView包含两个TextViews和一个Delete按钮。 When the button is clicked I'd like to be able to remove a record from the database, but I cannot seem to open the database. 单击该按钮后,我希望能够从数据库中删除一条记录,但是似乎无法打开数据库。 I am trying to open the database like so ... 我正在尝试像这样打开数据库...

db = SQLiteDatabase.openDatabase(com.red.myApp.Utils.database_name, null, Context.MODE_PRIVATE);

... this fails with this in the logcat ... ...这在logcat中失败了...

09-19 21:56:15.825: I/Database(4773): sqlite returned: error code = 14, msg = cannot open file at source line 25467
09-19 21:56:15.825: E/Database(4773): sqlite3_open_v2("BookMarksV20.db", &handle, 2, NULL) failed

So what's the deal? 那怎么办? Is just plain impossible to use SQLite in type of class other than a Activity or a Service? 在Activity或Service之外的类类型中使用SQLite显然是不可能的吗? What am I doing wrong here? 我在这里做错了什么?

Thank for taking a look :) 感谢您的光临:)

I hope this is useful to you.you have to put this code in your create() in activity. 我希望这对您有用。您必须在活动中将这段代码放入create()中。

    dbHelper = new DbHelper(this);
    dbHelper.createDataBase();
    sqLiteDatabase = dbHelper.openDataBase();

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

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