將map和泛型爲map的list集合插入到指定的數據庫表中

將HashMap

static SQLiteDatabase db;

    public int insertSQL(String table, HashMap<String, String> map) {
        long a;

        try {
            ContentValues values = new ContentValues();

            for (String key : map.keySet()) {
                values.put(key, map.get(key));
            }
             a = db.insert(table, null, values);
        } catch (SQLException e) {
            return 0;
        }
        return (int) a;
    }
 * 把 List<HashMap<String, String>> listMap插入到指定的數據庫表中;
 * @param table
 * @param listMap
 * @return

    public int insertSQL(String table, List<HashMap<String, String>> listMap) {
        try {
            db.beginTransaction();
            for (HashMap<String, String> map : listMap) {
                if (insertSQL(table, map) == -1) {
                    db.endTransaction();
                    return 0;
                }
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            db.endTransaction();
            Log.i("TAG", "insertSQL");
            return 0;
        }
        return 1;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章