1

I have been trying to create an SQLite database. But only one row is being added. (cursor.getCount() gives 1). Can anyone please tell me what is wrong with my code.

class Dbhelper extends SQLiteOpenHelper{

Dbhelper(Context context)
{
    super(context,"database00",null,1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
    db.execSQL("CREATE TABLE TABLE00 (_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                                       + "DISPLAY TEXT);");
    insertD(db,"a");
    insertD(db,"b");
    insertD(db,"c");

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

private static void insertD(SQLiteDatabase db,String display)
{
    ContentValues d=new ContentValues();
    d.put("DISPLAY",display);
    db.insert("TABLE00", null, d);
}}

Code that is accessing this info:

SQLiteOpenHelper Dbhelper = new Dbhelper(this); 
SQLiteDatabase db = Dbhelper.getReadableDatabase(); 
Cursor cursor=db.rawQuery("select * from TABLE00",null); 
cursor.moveToFirst(); 
int msg3 = cursor.getCount(); 
TextView textView = (TextView) findViewById(R.id.textView3); textView.setText("No of rows:"+msg3); 
cursor.close(); 
db.close();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nand gopal
  • 346
  • 3
  • 17
  • `cursor.getCount()` for what query? – laalto Mar 11 '16 at 09:42
  • 1
    `SQLiteOpenHelper Dbhelper = new Dbhelper(this); SQLiteDatabase db = Dbhelper.getReadableDatabase(); Cursor cursor=db.rawQuery("select * from TABLE00",null); cursor.moveToFirst(); int msg3 = cursor.getCount(); TextView textView = (TextView) findViewById(R.id.textView3); textView.setText("No of rows:"+msg3); cursor.close(); db.close();` – Nand gopal Mar 11 '16 at 09:46
  • @Nandgopal Good response. Put it in the answer section to get the due credit for it. – usamazf Mar 11 '16 at 09:51
  • textView displays "Now of rows:1" – Nand gopal Mar 11 '16 at 09:51
  • @UsamaZafar: No, I'm the one who asked the question. I created the table as mentioned above and did this in another class. Only 1 row is created(No of rows is 1). I don't understand why. – Nand gopal Mar 11 '16 at 09:54
  • @laalto: Could you find my mistake? – Nand gopal Mar 11 '16 at 09:55
  • Nothing wrong there. But are you sure there are more rows in the table? May be there is one? Also try getCount() before moving the cursor to the first record. – usamazf Mar 11 '16 at 10:02
  • @UsamaZafar i tried keeping getCount() before moving the cursor to the 1st record. it still gives 1. If i try to access the 2nd/3rd row i get `CursorIndexOutofBoundexception` . – Nand gopal Mar 11 '16 at 10:07
  • Capture the new row id from here `db.insert("TABLE00", null, d);` and print it out to logcat. – Knossos Mar 11 '16 at 10:26
  • `SQLiteOpenHelper#onCreate()` is run only once when the database file didn't exist. Uninstall your app to remove the database file and make `onCreate()` run again. – laalto Mar 11 '16 at 10:39
  • @laalto **I unistalled the app from the phone and ran it again** and now its proper. Thanks for the help and explation. Appreciate it. – Nand gopal Mar 11 '16 at 10:48

0 Answers0