0

EDIT: I still can't find a solution to this issue. For some reason, the database that is queried is empty, despite being full and in the correct place. If you can see any issue with my database helper or anything else that I could try, I would be very grateful.

I am developing an app that returns information from an SQLite database relating to species of bird. When I query this database, I am getting error messages: 'SQLiteLog: (1) no such column: 'size'' etc.

I have verified that my rawQuery() queries are well formed and should be returning the info that I expect by running the queries in an SQLite browser, and by consulting advice on Stackoverflow. I have tried to use the alternative database asset class, SQLiteAssetHelper, but have had the same problems as I have with SQLiteOpenHelper.

I am thinking it may be related to: the testing phone - Moto g6 play - this phone is not rooted. Could this be an issue migrating the database over to the phone for use by the app during testing? The formation of the database - this was populated by a python script written by me. Could some metadata etc be malformed or incompatible?

package com.example.newbuild;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class DatabaseHelper extends SQLiteOpenHelper {
    //db info
    private static final String DATABASE_NAME = "birdsDB.db";
    //    FileInputStream fis = new FileInputStream(new File(DATABASE_NAME));

// Log info for debugging:
private static final String TAG = "DatabaseHelper";

// set variables to name database
private static final int DATABASE_VERSION = 3;

// name of table 1:
private static final String MAIN_TABLE = "main";

// name of bird image table:
private static final String PIC_TABLE = "picLinks";

// names of MAIN columns:
private static final String ID = "id";
private static final String COMMON = "common";
private static final String SCINAME = "sciname";
private static final String FAMILY = "family";
private static final String BIRDCATEG = "category";
private static final String SIZE = "size";
private static final String DESC = "description";
private static final String RANGEPIC = "rangepicid";
private static final String SIGHTED = "sighted";

// names of BIRD IMAGE columns
private static final String BIRD_IMAGE_NO = "picKey";
private static final String BIRD_ID = "birdId";
private static final String IMAGE_LINK = "link";

private Context mContext;
private SQLiteDatabase mDB;


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.i(TAG, "now calling database helper");

}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.d(TAG, "attempting to create table from onCreate");
    String CREATE_MAIN_TABLE =
            "CREATE TABLE " +
                    MAIN_TABLE +
                    "(" +
                    ID + "INTEGER PRIMARY KEY," +
                    COMMON + " TEXT," +
                    SCINAME + " TEXT," +
                    FAMILY + "TEXT," +
                    BIRDCATEG + "TEXT," +
                    SIZE + "TEXT," +
                    DESC + "TEXT," +
                    RANGEPIC + "TEXT," +
                    SIGHTED + "TEXT" +
                    ")";
    db.execSQL(CREATE_MAIN_TABLE);
    Log.d("table", CREATE_MAIN_TABLE);
}


@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    Log.i(TAG, "now calling onUpgrade");
    db.execSQL("DROP TABLE IF EXISTS " + MAIN_TABLE);
    onCreate(db);

}

}

public ArrayList<String> getCategory(String[] name) {
        String TABLE_BIRDS = "main";
        ArrayList<String> categories = new ArrayList<>();

        if (name[0] != null) {
            Log.d(LOG_TAG, name[0]);
        } else {
            Log.d(LOG_TAG, "name[0] has not been passed");
        }
        Log.d(LOG_TAG, "SELECT DISTINCT " + name[0] + " FROM " + TABLE_BIRDS);

        Cursor x = db.rawQuery("SELECT DISTINCT " + name[0] + " FROM " + TABLE_BIRDS, null);
        if (x.getCount() == 0) {
            Log.i(LOG_TAG, "The cursor is not returning any data");
        }

        while (x.moveToNext()) {
            String category = x.getString(0);
            categories.add(category);
            Log.i("cursor loop", category);
        }
        return categories;
    }

When the above code is passed the string 'category', my database should return six strings of different categories of bird species. Instead, I find error messages including 'E/SQLiteLog: (1) no such column: category'.

grainman
  • 73
  • 7
  • Have you tried to un-install and re-install the app? – Xay Jul 12 '19 at 09:12
  • I tried the advice from here [link](https://stackoverflow.com/questions/28679346/how-do-i-uninstall-and-re-run-an-app-on-a-device-using-android-studio), which says to add ":app:uninstallAll" to the 'gradle-aware Make" instructions, but this caused an error to occur when running the app, so I removed the change. – grainman Jul 12 '19 at 09:17
  • try to increase the version of the database in your helper class – Xay Jul 12 '19 at 09:18
  • I changed the version in this class to 2 from 1. Should I try this further? – grainman Jul 12 '19 at 09:20
  • @AkshayRohilla, could you advise me the process of uninstalling and reinstalling the app? Thanks! – grainman Jul 12 '19 at 11:20
  • Change DATABASE_NAME in your "DatabaseHandler" class ( I faced same problem. But I suuceed by changing DATABASE_NAME.) – Xay Jul 12 '19 at 11:26
  • @AkshayRohilla I deleted the birdsDatabase.db, and replaced it in the same place with the same file, renamed to birdsDB.db. Unfortunately it didn't fix the problem! Could you suggest anything else I could try to connect this database? – grainman Jul 12 '19 at 11:45
  • can you edit the question and post your helper class code – Xay Jul 12 '19 at 11:59
  • @AkshayRohilla OK, please check above, I added the helper class. Thanks for your help with this! – grainman Jul 12 '19 at 12:05
  • try to pass "category" instead name[0] – Xay Jul 12 '19 at 12:11
  • Also I can see there is no value inside your table ,so can you add some values in it ,OR you try to fetch any other column and then let me know if error occurs or not – Xay Jul 12 '19 at 12:14
  • @AkshayRohilla "Cursor x = db.rawQuery("SELECT DISTINCT " + "category" + " FROM " + TABLE_BIRDS, null);" Still gives SQLiteLog: (1) No such column: category. – grainman Jul 12 '19 at 12:15
  • I have tried different column name strings, as well as the databaseUtils.dumpcursor() method, which shows that no data is being returned, even from "SELECT * FROM " + TABLE_BIRDS + " LIMIT 10" – grainman Jul 12 '19 at 12:17
  • 1
    Note: you have to uninstall and install app again to get any changes – Xay Jul 12 '19 at 12:19
  • 1
    have you tried the both solutions and uninstalled and installed the app again? – Xay Jul 12 '19 at 12:20
  • I'm sorry for my ignorance of this; do you mean just renaming the database .db file, or what? I can't just press the 'run' button again to try a new version of the code? – grainman Jul 12 '19 at 12:20
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196370/discussion-between-akshay-rohilla-and-grainman). – Xay Jul 12 '19 at 12:21
  • Thank you for taking the time to help me. Hopefully one day I can learn enough to help some people as well! – grainman Jul 12 '19 at 12:21
  • 1
    *the database that is queried is empty, despite being full and in the correct place* it seems that it is not full. In order to be full, it must have been copied from your app's assets folder to the correct folder in the device. Do you have code to do this? If not then you only create a database with an empty table. – forpas Jul 12 '19 at 13:50
  • @forpas I have now added the copyDatabase() method from [link](https://stackoverflow.com/questions/8052262/cant-copy-sqlite-database-from-assets). However, the InputStream is now unable to find the database and throwing a FileNotFoundException! I am going to open a new thread to ask about this. – grainman Jul 13 '19 at 03:39

1 Answers1

1

I am thinking it may be related to: the testing phone - Moto g6 play - this phone is not rooted. Could this be an issue migrating the database over to the phone for use by the app during testing?

I don't think so as you would typically get a table not found before a column not found.

I'd suggest temporarily changing the query to :-

Cursor x = db.rawQuery("SELECT * FROM " + TABLE_BIRDS + " LIMIT 10", null);

Followed by :-

DatabaseUtils.dumpCursor(x);

The first change will extract all columns from 10 rows from the table. The second line will output the data in the cursor to the log, including the column names.

I suspect that the column names are incorrect or missing. In which case you need to ensure that the file in the assets folder is correct, when it is make sure that you delete the database (delete the Apps data or uninstall the App) and then rerun the App.

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Thanks for this. I have run the query you suggest. The output is:" I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@cb00f00 I/System.out: <<<<<" – grainman Jul 12 '19 at 10:21
  • How do I go about deleting the Apps data / uninstall the app, then rerun the app? – grainman Jul 12 '19 at 10:22
  • @grainman Settings - Apps - Select the App. The table is empty. – MikeT Jul 12 '19 at 10:30
  • Do you mean File - Settings - ... in Android Studio? I can't find any 'Apps' tab here. – grainman Jul 12 '19 at 10:35