简体   繁体   中英

Insert JSON to SQLite table on Android

i have a JSONObject:

{"Table1":[{"row1":"1","B":"2"},{"row2":"1","B1":"2"}],"Table2":[{"C":"1","D":"1145"},{"C":"1","D":"1145"}],"Table3":[{"E":"62","F":"1"},{"C":"1","D":"1145"}]}

how can I insert into sqlite foreach table?

now use this code:

 for (Iterator<String> iterator = mJson.keys(); iterator.hasNext(); ) {
                    String tableName = iterator.next();
                    if (mJson.optJSONArray(tableName) != null) {
                        resetTable(tableName);
                        JSONArray tableArray = mJson.optJSONArray(tableName);
                        for (int i = 0; i < tableArray.length(); i++) {
                            JSONObject tableData = tableArray.getJSONObject(i);
                            ContentValues Values = new ContentValues();
                            for (Iterator<String> iter = tableData.keys(); iter.hasNext(); ) {
                                String key = iter.next();
                                Values.put(key, tableData.get(key).toString());
                            }
                            db.insert(tableName, null, Values);
                        }
                    }
                }

but i want fastest and better way

You can use ContentValues with beginTransaction into SQLite that is quite easy as well as faster then prepared statements

For this you have to create ContentValues Array previously or create Content values object into your loop. and pass into insert method.this solution solve your both of problem in one.

mDatabase.beginTransaction();
    try {
        for (ContentValues cv : values) {
            long rowID = mDatabase.insert(table, " ", cv);
            if (rowID <= 0) {
                throw new SQLException("Failed to insert row into ");
            }
        }
        mDatabase.setTransactionSuccessful();
        count = values.length;
    } finally {
        mDatabase.endTransaction();
    }

You can only pass Content Values Object like for loop and insert and in above code Transaction are used so it will speed up data base storage.

Use bulk insert:

for (Iterator<String> iterator = mJson.keys(); iterator.hasNext(); ) {
    String tableName = iterator.next();
    if (mJson.optJSONArray(tableName) != null) {
        resetTable(tableName);
        String sql = "INSERT INTO " + tableName + " VALUES (?);";
        SQLiteStatement statement = db.compileStatement(sql);
        db.beginTransaction();
        JSONArray tableArray = mJson.optJSONArray(tableName);
        for (int i = 0; i < tableArray.length(); i++) {
            JSONObject tableData = tableArray.getJSONObject(i);
            for (Iterator<String> iter = tableData.keys(); iter.hasNext(); ) {
                String key = iter.next();
                statement.clearBindings();
                statement.bindString(1,tableData.get(key).toString());
                statement.execute();
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM