Difference between revisions of "Phonebot developer's reference/Object reference/Managing databases in Phonebot applications"

From EtherWiki
Jump to: navigation, search
Line 5: Line 5:
 
* Use the [[Phonebot_developer%27s_reference/Object_reference#Database|Database]] object to manage fields and records in a database
 
* Use the [[Phonebot_developer%27s_reference/Object_reference#Database|Database]] object to manage fields and records in a database
 
* Use the [[Phonebot_developer%27s_reference/Object_reference#Record|Record]] object to manage field values in a record
 
* Use the [[Phonebot_developer%27s_reference/Object_reference#Record|Record]] object to manage field values in a record
 +
 +
Phonebot Plus provides a [[Phonebot_developer%27s_reference/Advanced_tools#Database_browser database browser]] to manage schema and data.
  
 
== Object reference ==
 
== Object reference ==

Revision as of 03:30, 21 June 2012

A Phonebot database is a collection of data accessed by a unique name. Databases map to tables in SQL. Database records are accessed by their zero-based index. Fields within records are accessed by their zero-based index or by their field name.

  • Use the Application object to manage databases in an application
    • database_exists(), delete_database(), get_database(), and get_database_names()
  • Use the Database object to manage fields and records in a database
  • Use the Record object to manage field values in a record

Phonebot Plus provides a Phonebot_developer's_reference/Advanced_tools#Database_browser database browser to manage schema and data.

Object reference

Properties

Syntax Description
exists True if the database has fields defined.
field_size The number of fields in the database.
name
size The number of records in the database.

Methods

Syntax Description
index = add_field(name) Returns zero-based index. -1 if the field cannot be added.
record = add_record()
clear()
delete_field(index | name) Delete the field. The values in the associated field in every record in the database will be deleted. This includes those records currently referenced by variables. The index value of the remaining fields will change if those fields have a higher index value than the field being deleted.
delete_record(index | record)
name = get_field_info(index)

index = get_field_info(name)

Logs error if index or name does not exist.
array = get_field_names()
record = get_record(index) Logs error if no record exits at the zero-based index.
record = get_record_by_id(id) Get the record based on its unique ID. Retrieved from record.id.

Usage:

/* Create a database and record */
db = application1.get_database("contacts")
db:database.add_field("name")
db:database.add_field("email")
first_rec = db:database.add_record()
first_rec:record.set_field("name", "Joe Bot")
first_rec:record.set_field("email", "jbot@android.com")

/* Loop through all fields in the database */
for (field_index = 0; field_index < db:database.field_size; field_index = field_index + 1)
    field_name = db:database.get_field(field_index)
end

/* Loop through all records in a database */
for (index = 0; index < db:database.size; index = index + 1)
    rec = db:database.get_record(index)
    name = rec:record.get_field("name")
    email = rec:record.get_field("email")
    for (field_index = 0; field_index < db:database.field_size; field_index = field_index + 1)
        field_value = rec:record.get_field(field_index)
    end
end

/* Delete the new record and database */
db:database.delete_record(first_rec)
application1.delete_database("contacts")