示例#1
0
  /**
   * @param tableName
   * @param projectionIn 需要返回的字段,null返回所有字段
   * @param selection where语句, 不包含where关键字,eg id=? and age=?
   * @param selectionArgs 对应where语句?的值 new String[]{String.valueOf(id), String.valueOf(age)}
   * @param groupBy
   * @param having
   * @param sortOrder 升序降序 id desc
   * @param limit 返回结果的数目
   * @return
   */
  protected Cursor query(
      String tableName,
      String[] projectionIn,
      String selection,
      String[] selectionArgs,
      String groupBy,
      String having,
      String sortOrder,
      String limit) {

    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    builder.setTables(tableName);
    Cursor cursor =
        builder.query(
            mDbHelper.getReadableDatabase(),
            projectionIn,
            selection,
            selectionArgs,
            groupBy,
            having,
            sortOrder,
            limit);

    return cursor;
  }
示例#2
0
 protected Cursor rawQuery(String sql, String[] args) {
   Cursor cursor = mDbHelper.getReadableDatabase().rawQuery(sql, args);
   if (cursor == null) {
     return null;
   } else if (!cursor.moveToFirst()) {
     cursor.close();
     return null;
   }
   return cursor;
 }
示例#3
0
 protected void batchInsert(String tableName, List<ContentValues> cvs) {
   SQLiteDatabase db = mDbHelper.getWritableDatabase();
   db.beginTransaction();
   try {
     for (ContentValues cv : cvs) {
       db.replace(tableName, null, cv);
     }
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
 }
示例#4
0
 protected Cursor query(
     String tableName,
     @Nullable String selection,
     @Nullable String[] selectionArgs,
     @Nullable String[] columns) {
   SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
   builder.setTables(tableName);
   Cursor cursor =
       builder.query(
           mDbHelper.getReadableDatabase(), columns, selection, selectionArgs, null, null, null);
   return cursor;
 }
示例#5
0
 protected long insert(String tableName, ContentValues cv) {
   SQLiteDatabase db = mDbHelper.getReadableDatabase();
   return db.insert(tableName, null, cv);
 }
示例#6
0
 public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
   SQLiteDatabase db = mDbHelper.getReadableDatabase();
   return db.update(table, values, whereClause, whereArgs);
 }
示例#7
0
 public int delete(String tableName, String whereClause, String[] whereArgs) {
   SQLiteDatabase db = mDbHelper.getReadableDatabase();
   int deleted = db.delete(tableName, whereClause, whereArgs);
   return deleted;
 }