无法从数据库中检索任何数据

huangapple go评论70阅读模式
英文:

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, &quot;code is in the Runnable&quot;);
final Map&lt;Integer, String&gt; backgroundmap = new HashMap&lt;&gt;();
DatabaseAccess mydatabase = DatabaseAccess.getInstance(getContext());
mydatabase.open();
Cursor cursor = mydatabase.SearchbyName();
if (cursor != null &amp;&amp; cursor.moveToFirst()) {
for (int i = 0; i &lt; cursor.getCount(); i++) {
Log.d(TAG, &quot;MainFragment code in cursor result:&quot; + cursor.getString(cursor.getColumnIndex(&quot;name&quot;)));
backgroundmap.put(cursor.getInt(cursor.getColumnIndex(&quot;id&quot;)), cursor.getString(cursor.getColumnIndex(&quot;name&quot;)));
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.... –

huangapple
  • 本文由 发表于 2020年9月23日 03:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64016314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定