Mark McCleane
Mark McCleane

Reputation: 31

Android SQLite error on cursor

I'm currently working on an android project and am stuck on this method. The cursor query works but anything after that does not. If I take out the 'returnCompany' method it runs so I'm guessing the error is inside the cursor query. The method is taking in a string parameter called 'companyNameParemeter'. FYI Android Studio is the text editor I'm using.

SQLiteDatabase db = this.getReadableDatabase();
    String returnString = "";

    Cursor cursor = db.rawQuery("Select * from " + CONTANT_INFO_AND_GPS_TABLE + " where " +
   KEY_COMPANY_NAME + " = ? ", new String[] {companyNameParamater});


    company returnCompany = new company(Integer.parseInt(cursor.getString(0)), cursor.getString(1),
            cursor.getString(2), cursor.getString(3),Double.parseDouble(cursor.getString(4) ),
             Double.parseDouble(cursor.getString(5)));

    /* returnString = ("Company is:\t" + returnCompany.getCompanyName() + ",\nContact name:\t" +
                returnCompany.getContactName() + ",\tContact No:\t" + returnCompany.getContactNumber()
                + ",\nGPS:\tLat:\t"
        + returnCompany.getGPS_Latitude() + "\tLong:\t" + returnCompany.getGPS_Longitude() );*/

        return returnString;

I checked LogCat and the only relevant error I could find was this

04-04 12:06:38.237 2032-1714/? E/GCoreUlr: Received null location result

Upvotes: 0

Views: 479

Answers (2)

MikeT
MikeT

Reputation: 57083

The error you show doesn't reflect the error you would typically get. However the code you have shown is clearly wrong and would result in an error because you are trying read a row when you are positioned at the start position of the cursor (aka beforeFirstRow).

You have to move to a row before you can get the data. As you only appear to have a single row, then using the Cursor method moveToFirst will either position you at the first (only) row. If the move cannot be made (no rows) then like all the Cursor move????? methods (moveTolast, moveToNext etc) a boolean false is returned (true if the move was made) hence it's use in within the if.

    SQLiteDatabase db = this.getReadableDatabase();
    String returnString = "";

    Cursor cursor = db.rawQuery("Select * from " + CONTANT_INFO_AND_GPS_TABLE + " where " + KEY_COMPANY_NAME + " = ? ", new String[] {companyNameParamater});

    if (cursor.moveToFirst()) { //<<<< Move the cursor to a row
        company returnCompany = new company(Integer.parseInt(cursor.getString(0)), 
        cursor.getString(1),
        cursor.getString(2), 
        cursor.getString(3),
        Double.parseDouble(cursor.getString(4)),
        Double.parseDouble(cursor.getString(5)));

        /* returnString = ("Company is:\t" + returnCompany.getCompanyName() + ",\nContact name:\t" +
                returnCompany.getContactName() + ",\tContact No:\t" + 
                returnCompany.getContactNumber()
                + ",\nGPS:\tLat:\t"
                + returnCompany.getGPS_Latitude() + "\tLong:\t" + 
                returnCompany.getGPS_Longitude() );
        */
    }

    return returnString;

P.S. Double.parseDouble(cursor.getString(4)) could be cursor.getDouble(4)

Upvotes: 0

N.K
N.K

Reputation: 1690

Try using

cursor.moveToFirst()

Right AFTER creating and filling it with db.rawquery or wichever call you use to get your data from your DB,

It will make sure your cursor is pointing at the first element you received, i may be wrong but i remember it's always pointing at the end of the data it contains when you fill it.

Upvotes: 2

Related Questions