下面列出了android.database.DatabaseUtils#InsertHelper ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void addFilterKeyword(int type, Collection<String> words) {
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), FilterTable.TABLE_NAME);
final int nameColumn = ih.getColumnIndex(FilterTable.NAME);
final int activeColumn = ih.getColumnIndex(FilterTable.ACTIVE);
final int typeColumn = ih.getColumnIndex(FilterTable.TYPE);
try {
getWsd().beginTransaction();
for (String word : words) {
ih.prepareForInsert();
ih.bind(nameColumn, word);
ih.bind(activeColumn, true);
ih.bind(typeColumn, type);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
private static void add(String accountId, List<String> list) {
if (list == null || list.size() == 0) {
return;
}
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), TopicTable.TABLE_NAME);
final int accountidColumn = ih.getColumnIndex(TopicTable.ACCOUNTID);
final int nameColumn = ih.getColumnIndex(TopicTable.TOPIC_NAME);
try {
getWsd().beginTransaction();
for (int i = 0; i < list.size(); i++) {
String name = list.get(i);
ih.prepareForInsert();
ih.bind(accountidColumn, accountId);
ih.bind(nameColumn, name);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void addUnreadNotification(String accountId, ArrayList<String> msgIds, UnreadDBType type) {
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), NotificationTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(NotificationTable.MSGID);
final int accountidColumn = ih.getColumnIndex(NotificationTable.ACCOUNTID);
final int typeColumn = ih.getColumnIndex(NotificationTable.TYPE);
try {
getWsd().beginTransaction();
for (String msg : msgIds) {
ih.prepareForReplace();
ih.bind(mblogidColumn, msg);
ih.bind(accountidColumn, accountId);
ih.bind(typeColumn, type.getValue());
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException ignored) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void addFilterKeyword(int type, Collection<String> words) {
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), FilterTable.TABLE_NAME);
final int nameColumn = ih.getColumnIndex(FilterTable.NAME);
final int activeColumn = ih.getColumnIndex(FilterTable.ACTIVE);
final int typeColumn = ih.getColumnIndex(FilterTable.TYPE);
try {
getWsd().beginTransaction();
for (String word : words) {
ih.prepareForInsert();
ih.bind(nameColumn, word);
ih.bind(activeColumn, true);
ih.bind(typeColumn, type);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void addUnreadNotification(String accountId, ArrayList<String> msgIds, UnreadDBType type) {
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), NotificationTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(NotificationTable.MSGID);
final int accountidColumn = ih.getColumnIndex(NotificationTable.ACCOUNTID);
final int typeColumn = ih.getColumnIndex(NotificationTable.TYPE);
try {
getWsd().beginTransaction();
for (String msg : msgIds) {
ih.prepareForReplace();
ih.bind(mblogidColumn, msg);
ih.bind(accountidColumn, accountId);
ih.bind(typeColumn, type.getValue());
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException ignored) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
private static void add(String accountId, List<String> list) {
if (list == null || list.size() == 0) {
return;
}
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), TopicTable.TABLE_NAME);
final int accountidColumn = ih.getColumnIndex(TopicTable.ACCOUNTID);
final int nameColumn = ih.getColumnIndex(TopicTable.TOPIC_NAME);
try {
getWsd().beginTransaction();
for (int i = 0; i < list.size(); i++) {
String name = list.get(i);
ih.prepareForInsert();
ih.bind(accountidColumn, accountId);
ih.bind(nameColumn, name);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void insert(UserBean msg, String ownerid) {
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
ContactTable.TABLE_NAME);
final int nameCol = ih.getColumnIndex(ContactTable.NAME);
final int avatarCol = ih.getColumnIndex(ContactTable.AVATAR);
final int uidCol = ih.getColumnIndex(ContactTable.UID);
final int genderCol = ih.getColumnIndex(ContactTable.GENDER);
final int ownerCol = ih.getColumnIndex(ContactTable.OWNERID);
final int rightCountCol = ih.getColumnIndex(ContactTable.RIGHT_COUNT);
final int missCountCol = ih.getColumnIndex(ContactTable.MISS_COUNT);
final int statueCountCol = ih.getColumnIndex(ContactTable.STATUE_COUNT);
try {
getWsd().beginTransaction();
ih.prepareForInsert();
ih.bind(nameCol, msg.getName());
ih.bind(avatarCol, msg.getAvatar());
ih.bind(uidCol, msg.getId());
ih.bind(genderCol, msg.getGender());
ih.bind(rightCountCol, msg.getRight_count());
ih.bind(missCountCol, msg.getMiss_count());
ih.bind(statueCountCol, msg.getStatue_count());
ih.bind(ownerCol, ownerid);
ih.execute();
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
e.printStackTrace();
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void insertList(ArrayList<UserBean> list, String ownerid) {
if (list == null || list.size() == 0) {
return;
}
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
ContactTable.TABLE_NAME);
final int nameCol = ih.getColumnIndex(ContactTable.NAME);
final int avatarCol = ih.getColumnIndex(ContactTable.AVATAR);
final int uidCol = ih.getColumnIndex(ContactTable.UID);
final int genderCol = ih.getColumnIndex(ContactTable.GENDER);
final int ownerCol = ih.getColumnIndex(ContactTable.OWNERID);
final int rightCountCol = ih.getColumnIndex(ContactTable.RIGHT_COUNT);
final int missCountCol = ih.getColumnIndex(ContactTable.MISS_COUNT);
final int statueCountCol = ih.getColumnIndex(ContactTable.STATUE_COUNT);
try {
getWsd().beginTransaction();
for (int i = 0; i < list.size(); i++) {
UserBean msg = list.get(i);
ih.prepareForInsert();
ih.bind(nameCol, msg.getName());
ih.bind(avatarCol, msg.getAvatar());
ih.bind(uidCol, msg.getId());
ih.bind(genderCol, msg.getGender());
ih.bind(rightCountCol, msg.getRight_count());
ih.bind(missCountCol, msg.getMiss_count());
ih.bind(statueCountCol, msg.getStatue_count());
ih.bind(ownerCol, ownerid);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
e.printStackTrace();
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void add(MessageListBean list, String accountId) {
if (list == null || list.getSize() == 0) {
return;
}
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), MyStatusTable.StatusDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(MyStatusTable.StatusDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(MyStatusTable.StatusDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(MyStatusTable.StatusDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void addCommentLineMsg(CommentListBean list, String accountId) {
Gson gson = new Gson();
List<CommentBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), CommentsTable.CommentsDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(CommentsTable.CommentsDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(CommentsTable.CommentsDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(CommentsTable.CommentsDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (CommentBean msg : msgList) {
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceCommentTable(accountId);
}
public static void addCommentLineMsg(CommentListBean list, String accountId) {
Gson gson = new Gson();
List<CommentBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
MentionCommentsTable.MentionCommentsDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(MentionCommentsTable.MentionCommentsDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(MentionCommentsTable.MentionCommentsDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(MentionCommentsTable.MentionCommentsDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (CommentBean msg : msgList) {
ih.prepareForInsert();
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceCommentTable(accountId);
}
private static void addHomeLineMsg(MessageListBean list, String accountId) {
if (list == null || list.getSize() == 0) {
return;
}
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), HomeTable.HomeDataTable.HOME_DATA_TABLE);
final int mblogidColumn = ih.getColumnIndex(HomeTable.HomeDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(HomeTable.HomeDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(HomeTable.HomeDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceHomeTable(accountId);
}
public static void addCommentLineMsg(CommentListBean list, String accountId) {
Gson gson = new Gson();
List<CommentBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), CommentsTable.CommentsDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(CommentsTable.CommentsDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(CommentsTable.CommentsDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(CommentsTable.CommentsDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (CommentBean msg : msgList) {
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceCommentTable(accountId);
}
public static void addCommentLineMsg(CommentListBean list, String accountId) {
Gson gson = new Gson();
List<CommentBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
CommentByMeTable.CommentByMeDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(CommentByMeTable.CommentByMeDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(CommentByMeTable.CommentByMeDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(CommentByMeTable.CommentByMeDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (CommentBean msg : msgList) {
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceCommentTable(accountId);
}
public static void add(MessageListBean list, String accountId) {
if (list == null || list.getSize() == 0) {
return;
}
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), MyStatusTable.StatusDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(MyStatusTable.StatusDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(MyStatusTable.StatusDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(MyStatusTable.StatusDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
public static void addRepostLineMsg(MessageListBean list, String accountId) {
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
int size = msgList.size();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), RepostsTable.RepostDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(RepostsTable.RepostDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(RepostsTable.RepostDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(RepostsTable.RepostDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (int i = 0; i < size; i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceRepostTable(accountId);
}
public static void addCommentLineMsg(CommentListBean list, String accountId) {
Gson gson = new Gson();
List<CommentBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
MentionCommentsTable.MentionCommentsDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(MentionCommentsTable.MentionCommentsDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(MentionCommentsTable.MentionCommentsDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(MentionCommentsTable.MentionCommentsDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (CommentBean msg : msgList) {
ih.prepareForInsert();
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceCommentTable(accountId);
}
private static void addHomeLineMsg(MessageListBean list, String accountId) {
if (list == null || list.getSize() == 0) {
return;
}
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(), HomeTable.HomeDataTable.HOME_DATA_TABLE);
final int mblogidColumn = ih.getColumnIndex(HomeTable.HomeDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(HomeTable.HomeDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(HomeTable.HomeDataTable.JSONDATA);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceHomeTable(accountId);
}
public static void insertList(ArrayList<StatueBean> msgList, String uid) {
if (msgList == null || msgList.size() == 0) {
return;
}
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
StatueTable.TABLE_NAME);
final int textCol = ih.getColumnIndex(StatueTable.TEXT);
final int img_pathCol = ih.getColumnIndex(StatueTable.IMG_PATH);
final int create_atCol = ih.getColumnIndex(StatueTable.CREATED_AT);
final int latCol = ih.getColumnIndex(StatueTable.LAT);
final int lngCol = ih.getColumnIndex(StatueTable.LNG);
final int miss_countCol = ih.getColumnIndex(StatueTable.MISS_COUNT);
final int right_countCol = ih.getColumnIndex(StatueTable.RIGHT_COUNT);
final int uidCol = ih.getColumnIndex(StatueTable.UID);
final int sidCol = ih.getColumnIndex(StatueTable.SID);
final int ownerIdCol = ih.getColumnIndex(StatueTable.OWNERID);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
StatueBean msg = msgList.get(i);
ih.prepareForInsert();
ih.bind(textCol, msg.getText());
ih.bind(img_pathCol, msg.getImg_path());
ih.bind(create_atCol, msg.getCreated_at());
ih.bind(latCol, msg.getLat());
ih.bind(lngCol, msg.getLat());
ih.bind(miss_countCol, msg.getMiss_count());
ih.bind(right_countCol, msg.getRight_count());
ih.bind(uidCol, msg.getUser().getId());
ih.bind(sidCol, msg.getId());
ih.bind(ownerIdCol, uid);
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
}
private static void addHomeLineMsg(MessageListBean list, String accountId, String groupId) {
if (list == null || list.getSize() == 0) {
return;
}
Gson gson = new Gson();
List<MessageBean> msgList = list.getItemList();
DatabaseUtils.InsertHelper ih = new DatabaseUtils.InsertHelper(getWsd(),
HomeOtherGroupTable.HomeOtherGroupDataTable.TABLE_NAME);
final int mblogidColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.MBLOGID);
final int accountidColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.ACCOUNTID);
final int jsondataColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.JSONDATA);
final int groupidColumn = ih.getColumnIndex(HomeOtherGroupTable.HomeOtherGroupDataTable.GROUPID);
try {
getWsd().beginTransaction();
for (int i = 0; i < msgList.size(); i++) {
MessageBean msg = msgList.get(i);
ih.prepareForInsert();
if (msg != null) {
ih.bind(mblogidColumn, msg.getId());
ih.bind(accountidColumn, accountId);
String json = gson.toJson(msg);
ih.bind(jsondataColumn, json);
ih.bind(groupidColumn, groupId);
} else {
ih.bind(mblogidColumn, "-1");
ih.bind(accountidColumn, accountId);
ih.bind(jsondataColumn, "");
ih.bind(groupidColumn, groupId);
}
ih.execute();
}
getWsd().setTransactionSuccessful();
} catch (SQLException e) {
} finally {
getWsd().endTransaction();
ih.close();
}
reduceHomeOtherGroupTable(accountId, groupId);
}