将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;
}