无法在 SQLite 中插入数据

Not able to insert data in SQLite

M 在插入来自 json 的数据时没有这样的 table 虽然首先我有商店注册详细信息并且它存储正确但不是这个数据正在存储。而且数据传递正确,但它显示没有这样的 table 退出我调试了很多次。 这是我的 Sqlite 代码

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();
        }

    }

这是我的方法,从 json m 存储数据到 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);

                    }

在您的 MobexInAppDb class 中,您已经创建了 TABLE_NAME = "customer",但在您插入 table 的方法中,您正在插入 [=21] =] 称为 "ButtonGroups",但您没有在您提供的代码中的任何地方创建。

您需要创建 table "ButtonGroups" 才能插入您的 json 值。

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();
    }

}