mardi 30 juin 2015

android sqlite database error _id does not exist using existing database

I am using an existing database to build a database in an Android app. I continue to get an error in the debug saying column stop_name does not exist:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bkane56.practice.practiceapp/com.bkane56.practice.practiceapp.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist

the schema for the data base is as follows:

CREATE TABLE trips (
    block_id char(40),
    route_id char(40),
    direction_id char(2),
    trip_headsign char(20),
    shape_id char(20),
    service_id char(20),
    _id char(20) PRIMARY KEY,
    FOREIGN KEY(route_id) references routes(_id),
    FOREIGN KEY(service_id) references calendar(_id)
);
CREATE TABLE calendar(
    _id char(40),
    start_date char(10),
    end_date char(10),
    monday char(1),
    tuesday char(1),
    wednesday char(1),
    thursday char(1),
    friday char(1),
    saturday char(1),
    sunday char(1)
);
CREATE TABLE routes (
    route_long_name char(200),
    route_type char(40),
    route_text_color char(40),
    agency_id char(40),
    _id char(40) PRIMARY KEY,
    route_color char(40),
    route_short_name char(40)
);
CREATE TABLE stops (
    stop_lat Decimal(9,6),
    zone_id char(30),
    stop_lon Decimal(9,6),
    _id char(30) PRIMARY KEY,
    stop_desc char(30),
    stop_name char(30),
    location_type char(30),
    stop_code char(30)
);
CREATE TABLE stop_times ( 
    trip_id char(40),
    arrival_time char(40), 
    departure_time char(40), 
    stop_id char(40), 
    stop_sequence char(40), 
    stop_headsign char(40), 
    pickup_type char(40), 
    drop_off_type char(40), 
    shape_dist_traveled char(40),
    timepoint char(20),
    FOREIGN KEY(stop_id) references stops(_id),
    FOREIGN KEY(trip_id) references trips(_id)
);
CREATE TABLE android_metadata (
    locale char(40)
);

The method to construct the database (common on StackOverflow) seems to be working, not sure if correctly. There is no error or notification that the database does not exist. Here is the method:

public void copydatabase() throws IOException {

        // Open your local db as the input stream
        InputStream myinput = mycontext.getAssets().open(DB_NAME);

        // Path to the created empty db
        String outfilename = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myoutput = new FileOutputStream(outfilename);

        // transfer byte from inputfile to outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myinput.read(buffer)) > 0) {
            myoutput.write(buffer, 0, length);
        }

        // Close the streams
        myoutput.flush();
        myoutput.close();
        myinput.close();

    }

This is the method (in my Database Helper file which extends SQLiteOpenHelper) I am using to get a list of the stop names. This is later used to populate a ViewList.

public Cursor getAllStops() {

        final String TABLE = "stops";
        final String[] COLUMN = new String[] {"stop_name as _id"};
        final String ORDER_BY = "stop_lon";
        String mypath = DB_PATH + DB_NAME;

        try {
            myDataBase = SQLiteDatabase.openDatabase(mypath, null,
                    SQLiteDatabase.OPEN_READONLY);
            Cursor c = myDataBase.query(TABLE, COLUMN, null, null, null, null, ORDER_BY, null);
//            Cursor c = myDataBase.rawQuery("select stop_name as _id from stops", null);
            if (c != null) {
                c.moveToFirst();
                return c;
            } else {
                System.out.println("unable to execute getAllStops, cursor = null!");
            }
        }
        finally {
            myDataBase.close();
        }
        return null;
    }

Is there a way to check the database that the used by the emulator. I cannot find the:

data/data/com.bkane56.practice.practiceapp/databases/metrolink.sqlite

Any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire