-4

I am creating an object of class which has Sqlite database methods but when I call that class, my constructor is called but call is not going inside onCreate method..

My call is again returned back to caller class ...please tell me what I do to take the call inside onCreate method

I am calling KotDBHelper class using:

QueryListBean queryListBean=new QueryListBean();
            queryListBean.setQueryList(queryList);

             db=new KotDBHelper(context,queryListBean);
             db.getWritableDatabase();

my code for sqlite helper class

    public class KotDBHelper extends SQLiteOpenHelper {

        private static final int DB_VERSION = 3; 
        private static final String DB_NAME = "eKOT";
        ArrayList<String> queryList;
        KotDBHelper objKotDBHelper;
        BeanInterface bean;
        /*private static final String TABLE_CATEGORIES="category_master";
        private static final String TABLE_KOT="kot_master";
        private static final String TABLE_ITEM="item_master";
        private static final String TABLE_KOT_DETAIL="kot_detail";
        private static final String TABLE_WAITER_MASTER="waiter_master";
        private static final String TABLE_TABLE_MASTER="table_master";
        private static final String TABLE_CONFIGURATION_MASTER="configuration_master";
        private static final String TABLE_USER_MASTER="user_master";*/


        public KotDBHelper(Context context,BeanInterface bean){
                    super(context, DB_NAME, null, DB_VERSION);
                    this.bean=bean;

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try{
                String address;
                BluetoothAdapter adpt=BluetoothAdapter.getDefaultAdapter();
                address=adpt.getAddress();
// casting bean here
            QueryListBean objBean=(QueryListBean)bean;
            objBean.getQueryList();


                AsyncClass objAsyncClass=new AsyncClass(objKotDBHelper,null,address);
                objAsyncClass.execute("");

                for(int i=0;i<queryList.size();i++){
                    db.execSQL(queryList.get(i));
                }
                /*db.execSQL(CREATE_TABLE_CATEGORIES);
                db.execSQL(CREATE_TABLE_ITEM);
                db.execSQL(CREATE_WAITER_MASTER);
                db.execSQL(CREATE_TABL_KOT);
                db.execSQL(CREATE_TABLE_KOT_DETAIL);
                db.execSQL(CREATE_TABLE_MASTER);
                db.execSQL(CREATE_CONFIGURATION_MASTER);
                db.execSQL(CREATE_USER_MASTER);
                db.execSQL("insert into table_master(table_no,curDate,type) values(1,'','Room')");
                db.execSQL("insert into table_master(table_no,curDate,type) values(10,'','Table')");
                db.execSQL("insert into user_master(username,password) values('admin','admin')");*/ 
            }catch(Exception e){
                e.printStackTrace();
            }
        }
user3407138
  • 15
  • 1
  • 6
  • 1
    onCreate method is only called when we access the database via getReadableDatabase () or getWritableDatabase () – Atul O Holic May 06 '14 at 05:04
  • show your code of class – raj May 06 '14 at 05:07
  • onCreate is called only once, when you create the database for the first time. If you really need to call onCreate, you can change the database version so that onUpgrade is called, and from that method you can call onCreate – Aditya Gupta May 06 '14 at 05:07
  • KotDBHelper db=new KotDBHelper(context,queryListBean); db.getReadableDatabase(); – user3407138 May 06 '14 at 05:08
  • show the complete code of `SQLiteHelper` class – SMR May 06 '14 at 05:13
  • I have posted the code@SMR – user3407138 May 06 '14 at 05:16
  • y use bean anyway? and theres nothing in the `queryList` at all. what else are you expecting? – SMR May 06 '14 at 05:23
  • You place wrong code in onCreate(). You can use execsql statements for executing the table not the Blutooth boject. Once the table is create then use object override concept for fetching or accesing the table in activity file. Then after use Bluettoth object. – jack May 06 '14 at 05:24
  • @user2009847 i have edited the code but tell me why its not calling OnCreate method in Sqlite helper class – user3407138 May 06 '14 at 06:01
  • ok i put the sample code for sqlitehelper class which is running successful in my application, you can check and modify it then let me know. – jack May 06 '14 at 06:03
  • @user2009847 what i need is I want queries to be executed from arraylist and then I am executing in for loop you can see – user3407138 May 06 '14 at 06:47
  • then first you check getting the arraylist value proper on sqlitehelper class?? – jack May 06 '14 at 06:50
  • may be this link is help to u http://stackoverflow.com/questions/21933736/android-get-single-value-from-hashmap-arraylist – jack May 06 '14 at 06:53
  • @user2009847 I am getting the values inside arraylist by putting in constructor but tell me why Its not calling onCreate method.... you can see my updated question from where I am calling the sqlite helper class – user3407138 May 06 '14 at 07:17

2 Answers2

0

This is sample code for creating database in android using sqlite helper class:

public class SQLite_Helper extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "mydb.db";

    // Contacts table name


    private static final String USER_TABLE = "User";
    public static final String TABLE_ACCOUNT = "Account";

     static String CREATE_TABLE_TRANSACTION = "CREATE TABLE " + TABLE_TRANSACTION + "("
                + TRAN_ID + " INTEGER PRIMARY KEY NOT NULL," + PAYEE + " TEXT," + CATEGORY + " TEXT," + AMOUNT + " NUMERIC,"
                + DUEDATE + " TEXT," + PAYFROM + " TEXT," + NOTES + " TEXT,"  + IS_REPEAT + " TEXT," + TYPE + " TEXT,"    + " FOREIGN KEY ("+TRAN_ID+") REFERENCES "+USER_TABLE+" ("+USER_ID+"));";




    String CREATE_USER_TABLE = "CREATE TABLE " + USER_TABLE + "("
            + USER_ID + "  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + FIRST_NAME + " TEXT not null unique," + LAST_NAME + " TEXT not null unique," + EMAIL + " TEXT not null unique,"
            + PASSWORD + " TEXT not null unique" + ")";

    static String CREATE_TABLE_ACCOUNT = "CREATE TABLE " + TABLE_ACCOUNT + "("
            + ACCT_ID + " INTEGER PRIMARY KEY NOT NULL," + ACCT_NAME + " TEXT,"
            + ACCT_HEAD_NAME + " TEXT," + OPEN_BAL + " TEXT,"+ ACCOUNT_EMAIL + " TEXT," + " FOREIGN KEY ("+ACCT_ID+") REFERENCES "+USER_TABLE+" ("+USER_ID+"));";



    public SQLite_Helper(Context context) {
        super(context, "login.db", null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(CREATE_TABLE_ACCOUNT);
        db.execSQL(CREATE_USER_TABLE);
        db.execSQL(CREATE_TABLE_TRANSACTION); 
    }
//      code or methods something here...
}
jack
  • 338
  • 1
  • 3
  • 29
0

Database helper onCreate() runs once when

  • opening the database with getWritableDatabase() or getReadableDatabase() and

  • the database file didn't exist.

Once onCreate() returns successfully without throwing, the database is considered created.

See for more: When is SQLiteOpenHelper onCreate() / onUpgrade() run?

Doing other stuff than database setup in onCreate() is incorrect. Just create your tables and populate them with some default values if any. Remove all other code from onCreate().

Hiding problems by catching exceptions is incorrect. If there's a problem setting up the database, onCreate() should propagate the problem (exception) to the framework. Remove the try-catch.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303