Skip to content
This repository was archived by the owner on Mar 25, 2020. It is now read-only.
Ricky Tobing edited this page Aug 26, 2013 · 3 revisions

IView represents VIEW. To create a VIEW use the Modeling interface:

...
IDatabase db = DbQuery.getDatabase("MyDb");
db.open(dbVersion, new SQLiteBuilder() {
   @Override
   public Context getContext() {
      return ViewTest.this.getContext();
   }

   @Override
   public void onModelCreate(IDatabase database, IDatabase.Modeling modeling) {
      modeling.add("Orders")
	      .addPrimaryKey("Id")
	      .add("Quantity", "INTEGER")
	      .add("ProductId", "INTEGER")
              .add("CustomerId", "INTEGER");
      modeling.add("Products")
              .addPrimaryKey("Id")
              .add("Name", "TEXT")
              .add("Price", "FLOAT");
      modeling.add("Customers")
	      .addPrimaryKey("Id")
	      .add("Name", "TEXT")
	      .add("Address", "TEXT");

      // create views
      modeling.addView("CustomerView")
              .as("SELECT * FROM Customers")
              .ifNotExists();
      modeling.addView("OrderView")
              .as("SELECT O.Id, O.Quantity, P.Name AS ProductName, P.Price AS ProductPrice, C.Name AS CustomerName")
              .append("FROM Orders O")
              .append("JOIN Products P ON P.Id = O.ProductId")
              .append("JOIN Customers C ON C.Id = O.CustomerId")
              .ifNotExists();
    }
});
...

Accessing VIEW

Similar to getting ITable, to get a IView object do the following:

...
IDatabase db = ...
db.getView("ViewName");
...

#Operations VIEW is read-only; therefore, you can only query data with SELECT Statement

#IView.count()

  • IView.count() returns the total records inside the view. Equivalent to calling SELECT COUNT(*) IN View in SQL
  • IView.count(String condition) returns the total records inside the view that matches the specified condition. Equivalent to calling SELECT COUNT(*) IN View WHERE...
  • IView.count(String whereClause, Object... args) returns the total records inside the table that matches the whereClause. This is more preferred way then calling count(condition).

#IView.has() This is a convenient method to check for a record existence in a table.

  • IView.has(int id) returns true if view has a record with the same id; false if otherwise
  • IView.has(String condition) returns true if view has a record or more that satisfy the condition; false if otherwise
  • IView.has(String whereClause, Object... args) returns true if view has a record or more that satisfy the specified whereClause; false if otherwise

Clone this wiki locally