如何在android中访问SQLite数据库时修复SQLiteOpenHelper.getDatabaseLocked错误?

时间:2014-02-24 10:55:04

标签: android sqlite

我正在尝试从Sqlite数据库中检索一些数据。但是当我尝试使用getTaskSent方法检索数据时,我收到以下错误。任何人都可以建议我解决以下错误吗?

  E/AndroidRuntime(23159):  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)

这是我的数据库代码:

public class Databasehandler extends SQLiteOpenHelper{
private static final String LOGCAT = null;

public Databasehandler(Context applicationcontext) {
    super(applicationcontext,"androidsqlite.db", null, 4);
    // TODO Auto-generated constructor stub
    Log.d(LOGCAT,"Database Created");

}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String query,query1,query2; 
    query = "CREATE TABLE userlogin ( phoneId INTEGER PRIMARY KEY,username TEXT,userid INTEGER,contacts TEXT UNIQUE)";
 query1 = "CREATE TABLE task ( TaskId INTEGER PRIMARY KEY,heading TEXT,desc TEXT,d1 TEXT,d2 TEXT,t1 TEXT,t2 TEXT,receiver TEXT,sender TEXT)";
 query2 = "CREATE TABLE me ( fbId INTEGER PRIMARY KEY,fbname TEXT,fbuserid INTEGER,fbpic BLOB,ph TEXT,email TEXT)"; 
 db.execSQL(query1);
    db.execSQL(query);
    db.execSQL(query2);

}
 public void inserttask(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values2 = new ContentValues();
    values2.put("heading", queryValues.get("heading"));
    values2.put("desc", queryValues.get("desc"));
    values2.put("d1", queryValues.get("d1"));
    values2.put("d2", queryValues.get("d2"));
    values2.put("t1", queryValues.get("t1"));
    values2.put("t2", queryValues.get("t2"));
    values2.put("sender", queryValues.get("sender"));
    values2.put("receiver", queryValues.get("receiver"));

    database.insert("task", null, values2);
    database.close();
}
  public ArrayList<HashMap<String, String>> getTaskSent() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM task";

    SQLiteDatabase database = this.getReadableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            //map.put("userid", cursor.getString(1));
            map.put("heading", cursor.getString(1));
            map.put("desc", cursor.getString(2));
            map.put("d1", cursor.getString(3));
            map.put("d2", cursor.getString(4));
            map.put("t1", cursor.getString(5));
            map.put("t2", cursor.getString(6)); 
            map.put("receiver", cursor.getString(7));                                                                                                           

            wordList.add(map);
        } while (cursor.moveToNext());
    }cursor.close();
    database.close();


    return wordList;
}

3 个答案:

答案 0 :(得分:1)

您传递给Context构造函数的Databasehandlernull

答案 1 :(得分:0)

您的代码看起来不错,不应该导致异常。

事实上,当一个活动(或任务)试图访问数据库进行写入而其他地方已经打开(其他活动,类等等)时,就会发生异常。

我建议您实施此处描述的解决方案:https://github.com/dmytrodanylyk/dmytrodanylyk/blob/gh-pages/articles/Concurrent%20Database%20Access.md

它基于两件事: 当您调用getReadableDatabase时,SQLite并不总是返回可读数据库,但可能返回一个可写数据库,并且在Open Helper单例中使用一个SQLiteOpenHelper单个实例(单例)和一个SQLiteDatabase实例。

解决方案可能看起来有点过分,但这是使用SQLite的最佳做法:您的应用程序可扩展性更高(查询速度快)和线程安全(防止查询冲突)。

我希望这会有所帮助,我仍然可以提出任何问题:)。

答案 2 :(得分:0)

public class RetriveMyApplicationContext extends Application {

   /*Don't forget to mention RetriveMyApplicationContext Class in Manifests    File otherwise it will throw NullPointer Exception
    <application
    android:name=".volley.RetriveMyApplicationContext"*/


    private static RetriveMyApplicationContext mRetriveMyApplicationContext;

        @Override
        public void onCreate() {
        super.onCreate();
        mRetriveMyApplicationContext = this;
}

      public static RetriveMyApplicationContext getInstance() {

         return mRetriveMyApplicationContext;
   }

       public static Context getAppContext() {

       return mRetriveMyApplicationContext.getApplicationContext();
}
}

首先创建Above Class并使用上面的类来传递Context 例如

DatabaseHandler d = new DatabaseHandler(RetriveMyApplicationContext.getAppContext());

最后添加到清单文件

<application
    android:name="<Your Package Name>.RetriveMyApplicationContext"
    ........


       > 
   <activity .....