Search code examples
androidandroid-studioandroid-sqlitesqliteopenhelper

Not able to insert data in SQLite


M getting no such table while inserting data from json While first i have store registration details and it stores correctly but y not this data is storing. and also the data is passing correctly but its shows no such table exits i debugged it many times. This is My Sqlite code

public SQLiteDatabase myDatabase;
    String TABLE_NAME = "customer";

    public MobexInAppDb(Context context) {
        super(context, "MyDb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query;
        //creating table
        query = "CREATE TABLE " + TABLE_NAME + "(id integer primary key autoincrement," + "f_name text," + "l_name text," + "mobile text," + "email text," + "add1 text," + "add2 text," + "country text," + "state text," + "city text," + "zip text" + ")";
        db.execSQL(query);
        //db.execSQL("create table"+ TABLE_NAME +  "( id integer primary key autoincrement," + "f_name text," + "l_name text," + "mobile text," + "email text," + "add1 text," + "add2 text," + "country text," + "state text," + "city text," + "zip text" + ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public void deleteButtonGroup(String tableName, String groupId, String sitecode) {
        try {
            myDatabase = this.getWritableDatabase();
            String where = "sitecode + '" + sitecode + "' and groupId ='" + groupId + "'";
            myDatabase.delete(tableName, where, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertButtonGroupData(String sitecode, String groupId, String groupName, String btnGrpStatus, String parentGroupId, String articleActive) {
        try {
            myDatabase = this.getWritableDatabase();
            ContentValues dataToInsert = new ContentValues();
            dataToInsert.put("sitecode", sitecode);
            dataToInsert.put("groupId", groupId);
            dataToInsert.put("groupName", groupName);
            dataToInsert.put("btnGrpStatus", btnGrpStatus);
            dataToInsert.put("parentGroupId", parentGroupId);
            dataToInsert.put("articleActive", articleActive);
            myDatabase.insertOrThrow("ButtonGroups", null, dataToInsert);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

This is my method where from json m storing data to sqlite

JSONArray buttnGroupDTOArray = response.getJSONArray("buttonGroupDTOArray");
                    for (int j = 0; j < buttnGroupDTOArray.length(); j++) {
                        JSONObject buttonGroupDTO = buttnGroupDTOArray.getJSONObject(j);
                        String groupID = buttonGroupDTO.getString("groupId");
                        String groupName = buttonGroupDTO.getString("groupName");
                        String btnGrpstatus = buttonGroupDTO.getString("status");
                        String parentGroupId = buttonGroupDTO.getString("parentGroupId");
                        String articleActive = buttonGroupDTO.getString("articleActive");
                        String siteCode = buttonGroupDTO.getString("sitecode");
                        mobexInAppDb.deleteButtonGroup("ButtonGroups", groupID, site);
                        mobexInAppDb.insertButtonGroupData(site,groupID,groupName,btnGrpstatus,parentGroupId,articleActive);

                    }

Solution

  • In your MobexInAppDb class, you have created TABLE_NAME = "customer" but in your method to insert into your table, you are inserting into a table called "ButtonGroups" that you haven't created anywhere in the code you provided.

    You need to create table "ButtonGroups" to be able to insert your json values.

    public SQLiteDatabase myDatabase;
    String TABLE_NAME = "customer";
    String BUTTON_GROUPS_TABLE_NAME = "ButtonGroups";
    
    public MobexInAppDb(Context context) {
        super(context, "MyDb", null, 1);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        String query, query_button_groups;
        //creating table
        query = "CREATE TABLE " + TABLE_NAME + "(id integer primary key autoincrement," + "f_name text," + "l_name text," + "mobile text," + "email text," + "add1 text," + "add2 text," + "country text," + "state text," + "city text," + "zip text" + ")";
        query_button_groups = "CREATE TABLE " + BUTTON_GROUPS_TABLE_NAME + "(id integer primary key autoincrement," + "f_name text," + "l_name text," + "mobile text," + "email text," + "add1 text," + "add2 text," + "country text," + "state text," + "city text," + "zip text" + ")";
    
        db.execSQL(query);
        db.execSQL(query_button_groups);
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    
    public void deleteButtonGroup(String tableName, String groupId, String sitecode) {
        try {
            myDatabase = this.getWritableDatabase();
            String where = "sitecode + '" + sitecode + "' and groupId ='" + groupId + "'";
            myDatabase.delete(tableName, where, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void insertButtonGroupData(String sitecode, String groupId, String groupName, String btnGrpStatus, String parentGroupId, String articleActive) {
        try {
            myDatabase = this.getWritableDatabase();
            ContentValues dataToInsert = new ContentValues();
            dataToInsert.put("sitecode", sitecode);
            dataToInsert.put("groupId", groupId);
            dataToInsert.put("groupName", groupName);
            dataToInsert.put("btnGrpStatus", btnGrpStatus);
            dataToInsert.put("parentGroupId", parentGroupId);
            dataToInsert.put("articleActive", articleActive);
            myDatabase.insertOrThrow("ButtonGroups", null, dataToInsert);
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }