我想從Sqlite數據庫檢索一些數據。但是,當我嘗試使用方法getTaskSent檢索數據時,出現以下錯誤。任何人都可以建議我解決以下錯誤?如何在嘗試訪問android中的SQLite數據庫時修復SQLiteOpenHelper.getDatabaseLocked錯誤?
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;
}
你好,當你調用getSentTask方法你能解釋一下嗎?它是否在'inserttask'之後被調用?在AsyncTask中? – KLiFF
我有3個活動。我已經在每個活動中創建了數據庫處理程序的一個實例。在我的addtask活動中,我有一個listview和一個按鈕。在listview中,我想使用getTaskSent方法從數據庫獲取數據。按鈕轉到另一個使用inserttask將數據插入數據庫的活動。 – user3256145