Android - Receiving "Index -1 requested with a size of 4" exception when exporting to file from SQLite

StackOverflow https://stackoverflow.com/questions/22261650

Question

I have a SQLite DB that I am exporting to a csv file using a cursor:

public Boolean exportDataToCSV(String outFileName) {

    Log.e("excel", "in exportDatabasecsv()");
    Boolean returnCode = false;

    String csvHeader = "";
    String csvValues = "";

    try {

        mDbHelper = new HDWDBHelper(context);
        mDbHelper.open();

        if (!DATABASE_DIRECTORY.exists()) {
            DATABASE_DIRECTORY.mkdirs();
        }
        Log.e("export fun:file name", outFileName);
        File outFile = new File(DATABASE_DIRECTORY, outFileName);
        FileWriter fileWriter = new FileWriter(outFile);
        Log.e("after FileWriter :file name", outFile.toString());
        BufferedWriter out = new BufferedWriter(fileWriter);

        Cursor cursor = mDbHelper.fetchAllOrders();
        // Log.e("excel", "cursor col count" + cursor.getCount());

        int col_count = cursor.getColumnCount();
        Log.e("col count", ""+col_count);
        csvHeader += "\"" + "_id" + "\",";
        csvHeader += "\"" + "itemnumber" + "\",";
        csvHeader += "\"" + "desc" + "\",";
        csvHeader += "\"" + "qty" + "\",";
        csvHeader += "\"" + "desc" + "\",";

        csvHeader += "\n";

        if (cursor != null) {
            out.write(csvHeader);
            while (!cursor.isAfterLast()) {
                // csvValues = Long.toString(cursor.getLong(0)) + ",";

                csvValues = cursor.getString(0) + ","; //id
                csvValues += cursor.getString(1) + ","; // name
                csvValues += cursor.getString(2) + ",\n"; // age
                csvValues += cursor.getString(3) + ",\n"; // age
                csvValues += cursor.getString(4) + ",\n"; // age

                out.write(csvValues);
                cursor.moveToNext();
            }
             Log.e("excel", "csvValues are:-  " + csvValues);

        }
        out.close();
        cursor.close();
        returnCode = true;
    } catch (Exception e) {
        returnCode = false;
        Log.e("Exception", e.getMessage());
    }

    //mDbHelper.close();
    return returnCode;
}

Here is where I am getting the data:

public Cursor fetchAllOrders() {        

    readCustNumFile2();
    String QTY0 = "qty > 0 and customernumber ="+customerNumber;
    Log.v("Query: ", QTY0);
    return mDb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_ITEMNUM,
            KEY_DESC, KEY_COST, KEY_QTY}, QTY0, null, null, null, null);


}

I am also receiving an "cursor-index-out-of-bounds-exception" when I debug.

Can anyone help? What more info would you need?

Was it helpful?

Solution 2

You should modify to the following

if (cursor != null && cursor.moveToFirst()) {
            out.write(csvHeader);
           do {
                // csvValues = Long.toString(cursor.getLong(0)) + ",";

                csvValues = cursor.getString(0) + ","; //id
                csvValues += cursor.getString(1) + ","; // name
                csvValues += cursor.getString(2) + ",\n"; // age
                csvValues += cursor.getString(3) + ",\n"; // age
                csvValues += cursor.getString(4) + ",\n"; // age

                out.write(csvValues);
            } while (cursor.moveToNext())
             Log.e("excel", "csvValues are:-  " + csvValues);

        }

OTHER TIPS

When you fill a Cursor, it is positioned BEFORE the first record (index is now -1).
So, if you don't move the Cursor to the first record (by calling cursor.moveToFirst();) before trying to get the values from the first row, you'll get that error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top