Search code examples
javaandroidandroid-sqlite

Can't retrieve any data from database


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();
        }
    }

Solution

  • 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.... –