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

From EtherWiki
Jump to: navigation, search
 
Line 1: Line 1:
 
+
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.
A 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 [[Phonebot_developer%27s_reference/Object_reference#Application|Application]] to manage databases in an application
 
* Use [[Phonebot_developer%27s_reference/Object_reference#Application|Application]] to manage databases in an application

Revision as of 02:47, 3 May 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 Application to manage databases in an application
  • Use Database to manage fields and records in a database
  • Use Record to manage field values in a record

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")