英文:
Can't retrieve any data from database
问题
我已将现有的数据库导入到我的Android Studio项目中。我正尝试在数据库上运行搜索查询,并将结果提供给我的RecyclerView,但似乎我从数据库中获得的都是null或类似的东西... 我尝试过删除WHERE参数,但没有起作用。也许这是一个愚蠢的问题,但这是我第一个实际项目,我有点迷失,非常感谢任何帮助。
class BackgroundRunnable implements Runnable {
private String name;
public BackgroundRunnable(String name) {
this.name = name;
}
@Override
public void run() {
Log.d(TAG, "代码在Runnable中");
final Map<Integer, String> backgroundmap = new HashMap<>();
DatabaseAccess mydatabase = DatabaseAccess.getInstance(getContext());
mydatabase.open();
Cursor cursor = mydatabase.SearchbyName();
if (cursor != null && cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
Log.d(TAG, "MainFragment中游标结果:" + cursor.getString(cursor.getColumnIndex("name")));
backgroundmap.put(cursor.getInt(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
cursor.close();
mainHandler.post(new Runnable() {
@Override
public void run() {
keyList.addAll(backgroundmap.keySet());
valueList.addAll(backgroundmap.values());
adapter.setvalues(keyList, valueList);
}
});
}
mydatabase.close();
}
}
英文:
I have imported an existing db to my Android Studio Project. I am trying to run a search query on database and feed the results to my recycler view but it seems all I am getting from my db is null or similar.... I tried removing the WHERE arguments but didn't work. Maybe it is a silly question but this is my first real world project and I am kind of lost, I appreciate any help.
class BackgroundRunnable implements Runnable {
private String name;
public BackgroundRunnable(String name) {
this.name = name;
}
@Override
public void run() {
Log.d(TAG, "code is in the Runnable");
final Map<Integer, String> backgroundmap = new HashMap<>();
DatabaseAccess mydatabase = DatabaseAccess.getInstance(getContext());
mydatabase.open();
Cursor cursor = mydatabase.SearchbyName();
if (cursor != null && cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
Log.d(TAG, "MainFragment code in cursor result:" + cursor.getString(cursor.getColumnIndex("name")));
backgroundmap.put(cursor.getInt(cursor.getColumnIndex("id")), cursor.getString(cursor.getColumnIndex("name")));
cursor.moveToNext();
}
cursor.close();
mainHandler.post(new Runnable() {
@Override
public void run() {
keyList.addAll(backgroundmap.keySet());
valueList.addAll(backgroundmap.values());
adapter.setvalues(keyList, valueList);
}
});
}
mydatabase.close();
}
}
答案1
得分: 0
经过痛苦的两天,我找到了一个答案。简而言之:在终端或类似工具中使用sqlite创建数据库,不要使用外部的 .exe 文件,比如DB Browser。我注意到,我通过DB Browser创建和导入的数据库看起来是空的,即使在DB Browser中检查会显示不同。我重新创建了表和数值,但没有用。然后我用另一个名称创建了一个新的数据库,并且修改了我的数据库助手类,解决得很顺利... –
英文:
After 2 days of pulling my hair out, I figured out an answer. In short: create the database using sqlite in the terminal or similar, don't use external .exe like DB Browser. I noticed that the database I create and imported from DB Browser appeared as empty even though a check in DB Browser would tell otherwise. I recreated the tables and values but to no avail. Then I created a new database with another name and changed my Database Helper class, worked like a charm.... –
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论