-1

I am simply try to create table, insert few records in it and then trying to retrieve the records from the db...what i have coded let me show it to you for more clear picture

 package com.example.istudy;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.dbtable.ProfileTable;
import com.example.dbtable.Sem5;

public class DBHandler extends SQLiteOpenHelper {

    private static final int VERSION = 1;
    private static final String DB_NAME = "iStudy";
    private static final String TABLE_NAME1 = "sem5";
    private static final String TABLE_NAME2 = "profile";


    //columns
    private static final String KEY_SUBJ = "sub_name";
    private static final String KEY_CHAP = "total_chapters";
    private static final String KEY_CHAP_COMPLETED = "chap_completed";
    private static final String KEY_CHAP_REMAINING = "chap_remaining";

    private static final String KEY_NAME = "name";
    private static final String KEY_SEM = "sem";
    private static final String KEY_COLG_TIMING = "colg_timing";
    private static final String KEY_STUDY_HOURS = "study_hours";
    private static final String KEY_TERM_START = "term_start";
    private static final String KEY_TERM_END = "term_end";


    public DBHandler(Context context) {
        super(context, DB_NAME, null, VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String create_table = "CREATE TABLE " + TABLE_NAME1 + " ( " + KEY_SUBJ + " TEXT PRIMARY KEY , " + KEY_CHAP + " INTEGER , " + 
        KEY_CHAP_COMPLETED + " INTEGER, " + KEY_CHAP_REMAINING + " INTEGER " + " )";
        db.execSQL(create_table);

        create_table = null;

        create_table = "CREATE TABLE " + TABLE_NAME2 + " ( " + KEY_NAME + " TEXT PRIMARY KEY, " + KEY_SEM + " INTEGER ,  " + KEY_COLG_TIMING + " TEXT ," + 
        KEY_STUDY_HOURS + " TEXT , " + KEY_TERM_START + " DATE , " + KEY_TERM_END + " DATE  )";
        db.execSQL(create_table);

        insertInSem5Table();

    }

    private void insertInSem5Table( ){
        String[] subName = {"ADBMS","CN","EVS","MP","TCS"};
        String insert_Query = "";
        SQLiteDatabase db = this.getWritableDatabase();

        for (int i = 0; i < subName.length; i++) {
            ContentValues values = new ContentValues();
            values.put(KEY_SUBJ, subName[i]);

                if( !(subName[i].equals("MP")) ){
                    values.put(KEY_CHAP, 8);

                }
                else{
                    values.put(KEY_CHAP, 7);
                }

                values.put(KEY_CHAP_COMPLETED, 0);

                if( !(subName[i].equals("MP")) ){
                    values.put(KEY_CHAP_COMPLETED, 8);

                }
                else{
                    values.put(KEY_CHAP_COMPLETED, 7);
                }
                db.insert(TABLE_NAME1, null, values);

        }
        db.close();


    }

    public List<Sem5> getAllRecordsOfSem5Table( ){
        List<Sem5> list = new ArrayList<Sem5>();
        String query = "SELECT * FROM " + TABLE_NAME1;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        if( cursor.moveToFirst() ){
            do{
                Sem5 sem5 = new Sem5();
                sem5.setKEY_SUBJ(cursor.getString(0));
                sem5.setKEY_CHAP(Integer.parseInt(cursor.getString(1)));
                sem5.setKEY_CHAP_COMPLETED(Integer.parseInt(cursor.getString(2)));
                sem5.setKEY_CHAP_REMAINING(Integer.parseInt(cursor.getString(3)));
                list.add(sem5);
            }while(cursor.moveToNext());
        }

        return list;

    }

    public List<ProfileTable> getAllRecordsOfProfileTable( ){
        List<ProfileTable> list = new ArrayList<ProfileTable>();
        String query = "SELECT * FROM " + TABLE_NAME2;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        if( cursor.moveToFirst() ){
            do{
                ProfileTable profileTable = new ProfileTable();
                profileTable.setKEY_NAME(cursor.getString(0));
                profileTable.setKEY_SEM(Integer.parseInt(cursor.getString(1)));
                Date colgTiming = null, studyHrs = null, termStart = null, termEnd = null;
                try {
                     colgTiming = java.text.DateFormat.getInstance().parse(cursor.getString(2));
                     studyHrs = java.text.DateFormat.getInstance().parse(cursor.getString(3));
                     termStart = java.text.DateFormat.getInstance().parse(cursor.getString(4));
                     termEnd = java.text.DateFormat.getInstance().parse(cursor.getString(5));
                     profileTable.setKEY_COLG_TIMING( colgTiming);
                     profileTable.setKEY_STUDY_HOURS(studyHrs);
                     profileTable.setKEY_TERM_START(termStart);
                     profileTable.setKEY_TERM_END(termEnd);
                     list.add(profileTable);

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }while(cursor.moveToNext());
        }

        return list;

    }




    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

calling db method using following code

public class Profile extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        DBHandler dbHandler = new DBHandler(this);
        Log.d("Insert : " , "INserting ...");
        List<Sem5> allRecordsOfSem5Table = dbHandler.getAllRecordsOfSem5Table();

    }


}

I am getting following error

06-29 06:55:59.993: E/AndroidRuntime(817): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.istudy/com.example.istudy.Profile}: java.lang.IllegalStateException: getDatabase called recursively

user2493303
  • 103
  • 3
  • 7
  • 2
    to read you normaly dont use getwritabledatabase but getreadabledatabase – Athul Harikumar Jun 29 '13 at 07:09
  • 1
    http://stackoverflow.com/questions/15955799/getdatabase-called-recursively – Vrashabh Irde Jun 29 '13 at 07:10
  • In getAllRecordsOfProfileTable( ),getAllRecordsOfSem5Table( )..u used getWritableDatabase() insteadOf getReadableDatabase(). – Bhoomika Brahmbhatt Jun 29 '13 at 07:12
  • 1
    k..as for now the above problem is solved ...but i ran into another one...my oncreate(..) method is not getting called and when i call "List allRecordsOfSem5Table = dbHandler.getAllRecordsOfSem5Table();" directly a call to getAllRecordsOfSem5Table() method is made without first envoking oncreate(...) method because of it i am getting NumberFormatException for parsing null instead of an int..how to solve this problem – user2493303 Jun 29 '13 at 07:54

1 Answers1

1

Look at this:

        onCreate(SQLiteDatabase db){
          ...
        insertInSem5Table();

        } 

... and this:

      private void insertInSem5Table( ){
      ....
      SQLiteDatabase db = this.getWritableDatabase();
       ...

You're trying to get a SQLiteDatabase recursively

Try :

        onCreate(SQLiteDatabase db){
          ...
        insertInSem5Table(db);

        } 

... and this:

      private void insertInSem5Table(SQLiteDatabase db ){
      ....
      SQLiteDatabase db = db //this of course is not necessary, just to point out the idea.
       ...
luisZavaleta
  • 1,160
  • 11
  • 21
  • k..thanks..but i have already fixed it ..now i have placed a log.d(..) statement in insertInSem5Table(...) method but that statement is not executing means the entire method is not executing ...but still i am getting half the values from the db in getAllRecordsOfSem5Table(...) method ...i am successfully getting columns 0,1 but result of column 2nd should be the result of column third. and the third column in null ...can u help.. if u want more info just let me know – user2493303 Jun 29 '13 at 08:29