Database is low-level API to access SQLite local database.
This API used internally by RHOM. To use RHOM, just define your models and partition databases will be created automatically.
This API is part of the coreapi
extension that is included automatically.
:::ruby
extensions: [“coreapi”]
Be sure to review the JavaScript API Usage guide for important information about using this API in JavaScript.
Be sure to review the Ruby API Usage guide for important information about using this API in Ruby.
Closes the database. The database will not be accessible until it is opened again.
Synchronous Return:
Method Access:
myObj.close()
@myObj.close()
Commit database transaction. Saves all updates to the database from the start of the transaction.
Synchronous Return:
Method Access:
myObject.commitTransaction()
Destroys a database table.
Parameters
Table name to destroy.
Synchronous Return:
Method Access:
myObject.destroyTable(STRING tableName)
Destroy a list of database tables.
Parameters
Include tables.
Exclude tables.
Synchronous Return:
Method Access:
myObject.destroyTables(HASH propertyMap)
Execute a series of sql statements included in the sqlStmt string parameter.
Parameters
The SQL statement.
Synchronous Return:
Method Access:
myObject.executeBatchSql(STRING sqlStmt)
Execute the sql statement specified in the method’s parameters.
Parameters
The SQL statement.
Array of the sql expressions.
Synchronous Return:
Array of Hashes. Each Hash item represents record from Database.
Method Access:
myObject.executeSql(STRING sqlStmt, ARRAY args)
This method is a constructor for this class. Instead of saying Rho.Database.initialize(dbPath,dbPartition) you would use new Rho.Database(dbPath,dbPartition). ex: var db = new Rho.Database(Rho.Application.databaseFilePath('test'), 'test');
Make sure you issue a .close()
when you are finished using the database. If the database file does not exist it will be created using a SQL schema: rhodes\platform\shared\db\res\db\syncdb.schema. Do not use predefined partition names: app, user, local. Do not open the same database file in different partitions. Do not use the same partition for different database files. Do not open the same file twice.
Parameters
The path to the database. Databases stored at the path provided by Application.databaseFilePath.
The database partition. Used as a file name for database and when connecting to RhoConnect server.
Synchronous Return:
Method Access:
var myObj = new Rho.Database(STRING dbPath, STRING dbPartition)
@myObj = Rho::Database.new(STRING dbPath, STRING dbPartition)
Will return true or false if the specified table exists in the current database.
Parameters
The name of the table.
Synchronous Return:
Method Access:
myObject.isTableExist(STRING tableName)
Rollback database transaction. This will cancel any pending actions to the database that were executed since the last Start and before a commit.
Synchronous Return:
Method Access:
myObject.rollbackTransaction()
Start database transaction. All operations will not be the saved to the database until a commit is executed.
Synchronous Return:
Method Access:
myObject.startTransaction()
To insert/update multiple object/models use database transactions. This is the most performant method to initialize your application with a large set of data.
var db = Rho.Database; db.startTransaction(); try { for (var index in items) { // create hash of attribute/value pairs data = { field1 : item[index].value1, field2 : item[index].value2 }; // Creates a new itemModel object and saves it new_item = itemModel.create(data); } db.commitTransaction(); } catch { db.rollbackTransaction(); }
db = Rho::Database.new db.startTransaction begin items.each do |item| # create hash of attribute/value pairs data = { :field1 => item['value1'], :field2 => item['value2'] } # Creates a new itemModel object and saves it new_item = itemModel.create(data) end db.commitTransaction rescue db.rollbackTransaction end
The following example opens the database using the constructor
method: .initialize. It then closes the database using the destructor method .close()
var db = new Rho.Database(Rho.Application.databaseFilePath('test'), 'test'); db.close();
db = Rho::Database.new(Rho::Application.databaseFilePath('test'), 'test') db.close()
Destroy multiple tables. The following example opens the database using the constructor
method: .initialize.
var db = new Rho.Database(...); // destroy all tables in database db.destroyTables({include: [], exclude: []}); // destroy specified tables db.destroyTables({include: ['table1', 'table2'], exclude: []}); // destroy all but specified tables db.destroyTables({include: [], exclude: ['table1', 'table2']});
db = Rho::Database.new(...) # destroy all tables in database db.destroyTables(:include => [], :exclude => []) # destroy specified tables db.destroyTables(:include => ['table1', 'table2'], :exclude => []) # destroy all but specified tables db.destroyTables(:include => [], :exclude => ['table1', 'table2'])