您好我正在尝试将首选选项添加到我的应用程序中我开始使用sqlite来保存图像的URL但是我在使用此代码将图片网址添加到收藏夹时遇到问题:
public void AddtoFavorite(Post pj) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_IMAGE_CATNAME, pj.getName());
values.put(KEY_IMAGE_URL, pj.getImgUrl());
db.insert(TABLE_NAME, null, values);
db.close();
}
它已成功添加,但是当我想检查图像URL是否存在于数据库中时
public List<Post> getFavRow(String url) {
List<Post> dataList = new ArrayList();
@SuppressLint("Recycle") Cursor cursor = getWritableDatabase().rawQuery("SELECT * FROM Favorite WHERE imageurl = '" + url + "'", null);
if (cursor.moveToFirst()) {
do {
Post contact = new Post();
contact.setName(cursor.getString(0));
contact.setImgUrl(cursor.getString(1));
dataList.add(contact);
} while (cursor.moveToNext());
}
return dataList;
}
总是我得到空列表。
答案 0 :(得分:0)
你应该在你的getFavRow()函数中使用getReadableDatabase()而不是getWritableDatabase()
SQLiteDatabase db = this.getReadableDatabase();
答案 1 :(得分:0)
尝试
WHERE imageurl LIKE'%“+ url +”%'“
答案 2 :(得分:0)
试试这个
public List<Post> getFavRow(String url) {
List<Post> dataList = new ArrayList();
String[] selectQuery = {"YOUR", "TABLE", "COLUMNS"};
String selection = "IMAGEURL = ?";
String[] selectionArgs = {url};
Cursor cursor = database.query(yourTable, selectQuery, selection, selectionArgs, null, null, null);
while(cursor.moveToNext()){
Post contact = new Post();
contact.setName(cursor.getString(cursor.getColumnIndex("NAME")));
contact.setImgUrl(cursor.getString(cursor.getColumnIndex("IMAGEURL")));
dataList.add(contact);
}
cursor.close();
return dataList;
}