This repository was archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
IView
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();
}
});
...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 callingSELECT COUNT(*) IN Viewin SQL -
IView.count(String condition)returns the total records inside the view that matches the specified condition. Equivalent to callingSELECT COUNT(*) IN View WHERE... -
IView.count(String whereClause, Object... args)returns the total records inside the table that matches thewhereClause. This is more preferred way then callingcount(condition).
#IView.has()
This is a convenient method to check for a record existence in a table.
-
IView.has(int id)returnstrueif view has a record with the sameid;falseif otherwise -
IView.has(String condition)returnstrueif view has a record or more that satisfy the condition;falseif otherwise -
IView.has(String whereClause, Object... args)returnstrueif view has a record or more that satisfy the specifiedwhereClause; false if otherwise