Archie.bpgc
Archie.bpgc

Reputation: 24012

android: something wrong about the cursor row counts

I am using this code in the DatabaseHandler:

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "wic";
    private static final String TABLE_NAME = "products";
    private static final String KEY_NAME = "pname";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_PRODUCTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_NAME + " TEXT" + ")";
        db.execSQL(CREATE_PRODUCTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        // Create tables again
        onCreate(db);
    }

    public void addProducts(String product_name) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, product_name); // Product Name
        // Inserting Row
        db.insert(TABLE_NAME, null, values);
        db.close(); // Closing database connection
    }

    public String getProducts() {

        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        Cursor cursor = db.rawQuery(selectQuery, null);
        String s = "default";
        if (cursor.moveToFirst()) {
            int n = cursor.getCount();
            Log.d("cursor count", "++  " + n);
            s = cursor.getString(0);
        }
        cursor.close();
        return s;
    }
}

I am adding only 1 string to the db. using this :

DatabaseHandler db = new DatabaseHandler(this);
db.addProducts("1st product");

but i get this exception:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5120

not while adding, but when trying to get values using db.getProducts();

cursor.getCounts gives me value = 5120;

I am using SQLite for the first time in android, i have no idea about this:

Thank You

Upvotes: 0

Views: 1849

Answers (3)

iTurki
iTurki

Reputation: 16398

You need to add an if-condition:

if(cursor.moveToFirst()) {
    int n = cursor.getCount();
    String s = cursor.getString(0);
}

Why? Cursors (initially) begin at row index -1 (before the first row). So, calling moveToFirst() will moves you to index 0 (the first row).

Upvotes: 2

user370305
user370305

Reputation: 109237

The line you missing is,

cursor.moveToFirst();

something in your code:

Cursor cursor = db.rawQuery(selectQuery, null);
int n = cursor.getCount();
cursor.moveToFirst();
int n = cursor.getCount();
String s = cursor.getString(0);

Upvotes: 3

Th0rndike
Th0rndike

Reputation: 3436

Use :

cursor.moveToFirst();

right after you receive the data in this line:

Cursor cursor = db.rawQuery(selectQuery, null);

Upvotes: 1

Related Questions