简体   繁体   中英

Android Studio - unable to retreive data from preinstalled sqlite database

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'.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM