Java Code Examples for android.arch.persistence.db.SupportSQLiteDatabase#insert()

The following examples show how to use android.arch.persistence.db.SupportSQLiteDatabase#insert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BriteDatabase.java    From sqlbrite with Apache License 2.0 6 votes vote down vote up
/**
 * Insert a row into the specified {@code table} and notify any subscribed queries.
 *
 * @see SupportSQLiteDatabase#insert(String, int, ContentValues)
 */
@WorkerThread
public long insert(@NonNull String table, @ConflictAlgorithm int conflictAlgorithm,
    @NonNull ContentValues values) {
  SupportSQLiteDatabase db = getWritableDatabase();

  if (logging) {
    log("INSERT\n  table: %s\n  values: %s\n  conflictAlgorithm: %s", table, values,
        conflictString(conflictAlgorithm));
  }
  long rowId = db.insert(table, conflictAlgorithm, values);

  if (logging) log("INSERT id: %s", rowId);

  if (rowId != -1) {
    // Only send a table trigger if the insert was successful.
    sendTableTrigger(Collections.singleton(table));
  }
  return rowId;
}
 
Example 2
Source File: Migration_16_17.java    From notSABS with MIT License 5 votes vote down vote up
@Override
public void migrate(SupportSQLiteDatabase database) {

    database.execSQL("CREATE TABLE PolicyPackage " +
            "(id TEXT PRIMARY KEY, " +
            "name TEXT NOT NULL, " +
            "createdAt INTEGER, " +
            "updatedAt INTEGER) ");

    Date currentDate = new Date();
    ContentValues contentValues = new ContentValues();
    contentValues.put("id", "default-policy");
    contentValues.put("name", "Default Policy");
    contentValues.put("createdAt", currentDate.getTime());
    contentValues.put("updatedAt", currentDate.getTime());
    database.insert("PolicyPackage", SQLiteDatabase.CONFLICT_REPLACE, contentValues);

    database.execSQL("CREATE TABLE AppPermission " +
            "(id INTEGER PRIMARY KEY, " +
            "packageName TEXT NOT NULL, " +
            "permissionName TEXT NOT NULL, " +
            "permissionStatus INTEGER DEFAULT 0, " +
            "policyPackageId TEXT DEFAULT 'default-policy', " +
            "FOREIGN KEY (policyPackageId) REFERENCES PolicyPackage(id))");
    database.execSQL("CREATE UNIQUE INDEX app_permission_policy_package_idx " +
            "ON AppPermission (packageName, permissionName, policyPackageId)");

    database.execSQL("CREATE TABLE DisabledPackage " +
            "(id INTEGER PRIMARY KEY, " +
            "packageName TEXT NOT NULL, " +
            "policyPackageId TEXT DEFAULT 'default-policy', " +
            "FOREIGN KEY (policyPackageId) REFERENCES PolicyPackage(id))");
    database.execSQL("CREATE UNIQUE INDEX disabled_package_policy_package_idx " +
            "ON DisabledPackage (packageName, policyPackageId)");
}
 
Example 3
Source File: Migration_16_17.java    From SABS with MIT License 5 votes vote down vote up
@Override
public void migrate(SupportSQLiteDatabase database) {

    database.execSQL("CREATE TABLE PolicyPackage " +
            "(id TEXT PRIMARY KEY, " +
            "name TEXT NOT NULL, " +
            "createdAt INTEGER, " +
            "updatedAt INTEGER) ");

    Date currentDate = new Date();
    ContentValues contentValues = new ContentValues();
    contentValues.put("id", "default-policy");
    contentValues.put("name", "Default Policy");
    contentValues.put("createdAt", currentDate.getTime());
    contentValues.put("updatedAt", currentDate.getTime());
    database.insert("PolicyPackage", SQLiteDatabase.CONFLICT_REPLACE, contentValues);

    database.execSQL("CREATE TABLE AppPermission " +
            "(id INTEGER PRIMARY KEY, " +
            "packageName TEXT NOT NULL, " +
            "permissionName TEXT NOT NULL, " +
            "permissionStatus INTEGER DEFAULT 0, " +
            "policyPackageId TEXT DEFAULT 'default-policy', " +
            "FOREIGN KEY (policyPackageId) REFERENCES PolicyPackage(id))");
    database.execSQL("CREATE UNIQUE INDEX app_permission_policy_package_idx " +
            "ON AppPermission (packageName, permissionName, policyPackageId)");

    database.execSQL("CREATE TABLE DisabledPackage " +
            "(id INTEGER PRIMARY KEY, " +
            "packageName TEXT NOT NULL, " +
            "policyPackageId TEXT DEFAULT 'default-policy', " +
            "FOREIGN KEY (policyPackageId) REFERENCES PolicyPackage(id))");
    database.execSQL("CREATE UNIQUE INDEX disabled_package_policy_package_idx " +
            "ON DisabledPackage (packageName, policyPackageId)");
}
 
Example 4
Source File: TestDb.java    From sqlbrite with Apache License 2.0 5 votes vote down vote up
@Override public void onCreate(@NonNull SupportSQLiteDatabase db) {
  db.execSQL("PRAGMA foreign_keys=ON");

  db.execSQL(CREATE_EMPLOYEE);
  aliceId = db.insert(TABLE_EMPLOYEE, CONFLICT_FAIL, employee("alice", "Alice Allison"));
  bobId = db.insert(TABLE_EMPLOYEE, CONFLICT_FAIL, employee("bob", "Bob Bobberson"));
  eveId = db.insert(TABLE_EMPLOYEE, CONFLICT_FAIL, employee("eve", "Eve Evenson"));

  db.execSQL(CREATE_MANAGER);
  db.insert(TABLE_MANAGER, CONFLICT_FAIL, manager(eveId, aliceId));
}
 
Example 5
Source File: DbCallback.java    From sqlbrite with Apache License 2.0 4 votes vote down vote up
@Override public void onCreate(SupportSQLiteDatabase db) {
  db.execSQL(CREATE_LIST);
  db.execSQL(CREATE_ITEM);
  db.execSQL(CREATE_ITEM_LIST_ID_INDEX);

  long groceryListId = db.insert(TodoList.TABLE, CONFLICT_FAIL, new TodoList.Builder()
      .name("Grocery List")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(groceryListId)
      .description("Beer")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(groceryListId)
      .description("Point Break on DVD")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(groceryListId)
      .description("Bad Boys 2 on DVD")
      .build());

  long holidayPresentsListId = db.insert(TodoList.TABLE, CONFLICT_FAIL, new TodoList.Builder()
      .name("Holiday Presents")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(holidayPresentsListId)
      .description("Pogo Stick for Jake W.")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(holidayPresentsListId)
      .description("Jack-in-the-box for Alec S.")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(holidayPresentsListId)
      .description("Pogs for Matt P.")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(holidayPresentsListId)
      .description("Cola for Jesse W.")
      .build());

  long workListId = db.insert(TodoList.TABLE, CONFLICT_FAIL, new TodoList.Builder()
      .name("Work Items")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(workListId)
      .description("Finish SqlBrite library")
      .complete(true)
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(workListId)
      .description("Finish SqlBrite sample app")
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder()
      .listId(workListId)
      .description("Publish SqlBrite to GitHub")
      .build());

  long birthdayPresentsListId = db.insert(TodoList.TABLE, CONFLICT_FAIL, new TodoList.Builder()
      .name("Birthday Presents")
      .archived(true)
      .build());
  db.insert(TodoItem.TABLE, CONFLICT_FAIL, new TodoItem.Builder().listId(birthdayPresentsListId)
      .description("New car")
      .complete(true)
      .build());
}