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

From EtherWiki
Jump to: navigation, search
 
(6 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
* Use the [[Phonebot_developer%27s_reference/Object_reference#Application|Application]] object to manage databases in an application
 
* Use the [[Phonebot_developer%27s_reference/Object_reference#Application|Application]] object to manage databases in an application
 +
** database_exists(), delete_database(), get_database(), and get_database_names()
 
* 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 ==
 +
 +
=== Database ===
  
 
Properties
 
Properties
Line 63: Line 70:
 
|}
 
|}
  
Usage:
+
=== Record ===
 +
 
 +
Represents a record in a specific database.
 +
 
 +
Properties
 +
 
 +
{|class="wikitable sortable"
 +
|-
 +
!Syntax
 +
!Description
 +
|-
 +
|<code>id</code>
 +
|The unique ID for the record. Read-only.
 +
|-
 +
|}
 +
 
 +
Methods
 +
 
 +
{|class="wikitable sortable"
 +
|-
 +
!Syntax
 +
!Description
 +
|-
 +
|<code>field = get_field(index <nowiki>|</nowiki> name)</code>
 +
|
 +
|-
 +
|<code>array = get_fields()</code>
 +
|
 +
|-
 +
|<code>set_field(index <nowiki>|</nowiki> name, value)</code>
 +
|
 +
|-
 +
|}
 +
 
 +
== Usage ==
  
 
  /* Create a database and record */
 
  /* Create a database and record */

Latest revision as of 11:29, 7 August 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 database browser to manage schema and data.

Object reference

Database

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.

Record

Represents a record in a specific database.

Properties

Syntax Description
id The unique ID for the record. Read-only.

Methods

Syntax Description
field = get_field(index | name)
array = get_fields()
set_field(index | name, value)

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