-
Notifications
You must be signed in to change notification settings - Fork 4
SQL Record
Using SQL Record objects SQL Yoga can create, retrieve, update and delete records from a database for you very easily. SQL Yoga can also create hierarchal SQL Record object arrays that represent the relationships in your database.
A SQL Record object is a simple LiveCode array with special keys prefixed with @. These keys help SQL Yoga locate the table in the database that the record represents among other things.
A SQL Record object that represents a single record looks something like this:
tRecordA["@table"] = lessons
tRecordA["@connection"] = development
tRecordA["@database"] = default
tRecordA["id"] = 5
tRecordA["name"] = lesson title
tRecordA["description"] = lesson description
A SQL Record object can also represent many records in the database using multi-dimensional arrays. Here is what a SQL Record object with 3 records might look like:
tRecordA[1]["@table"] = lessons
tRecordA[1]["@connection"] = development
tRecordA[1]["@database"] = default
tRecordA[1]["id"] = 5
tRecordA[1]["name"] = lesson 1 title
tRecordA[1]["description"] = lesson 1 description
tRecordA[2]["@table"] = lessons
tRecordA[2]["@connection"] = development
tRecordA[2]["@database"] = default
tRecordA[2]["id"] = 7
tRecordA[2]["name"] = lesson 2 title
tRecordA[2]["description"] = lesson 2 description
tRecordA[3]["@table"] = lessons
tRecordA[3]["@connection"] = development
tRecordA[3]["@database"] = default
tRecordA[3]["id"] = 8
tRecordA[3]["name"] = lesson 3 title
tRecordA[3]["description"] = lesson 3 description
You can use SQL Record objects to quickly find records in the database by using sqlrecord_find
.
sqlrecord_find
allows you to find records in a particular table in the database. The second parameter is what defines the search conditions. You can pass in an integer or name:value,name:value
pairs in which case the primary key field of the table is searched and a single record is returned.
## Look for a lessons record that has an 'id' of 1 (id is the primary key field).
## Store the found SQL Record object in tRecordA.
## If no records are returned then the keys of tRecordA will be empty.
sqlrecord_find "lessons", 1, tRecordA
For more advanced searches you can pass in an array that contains any properties that you can set on a SQL Query object.
## Advanced search using a parameters array
## Store the found SQL Record(s) in tRecordsA as a numerically indexed array.
put "id is in :1" into tParamsA["conditions"]
put "1,2" into tParamsA["condition bindings"][1]
put "id ASC" into tParamsA["order by"]
sqlrecord_find "lessons", tParamsA, tRecordsA
You can create records in a database using sqlrecord_create
.
The first step is to create a SQL Record object:
put sqlrecord_createObject("lessons") into tRecordA
## tRecordA is now an array that includes all fields in the 'lessons' table.
Now that you have a SQL Record object you can set the column values for the new record. In this example the title
column and description
column are being assigned values.
sqlrecord_set tRecordA, "title", "My Lesson"
sqlrecord_set tRecordA, "description", "A short lesson I made."
Once you are done assigning values to the columns you can create the record in the database.
sqlrecord_create tRecordA
put it into theAffectedRows
## In addition to the other column values, tRecordA now contains the database value for the 'id' (primary key) column.
The first step is to create a SQL Record object:
## Create SQL Record object by getting record from database.
## Store the found record in tRecordA
sqlrecord_find "lessons", 1, tRecordA
sqlrecord_set tRecordA, "title", "My New Lesson"
sqlrecord_set tRecordA, "description", "A short lesson I made and modified."
sqlrecord_update tRecordA
put it into theAffectedRows
You can search for or create a SQL Record object:
## Create SQL Record object by getting record from database.
## Store the found record in tRecordA
sqlrecord_find "lessons", 1, tRecordA
put sqlrecord_createObject("lessons") into tRecordA
sqlrecord_set tRecordA, "id", 12 ## You must set the primary key column value(s)
sqlrecord_delete tRecordA
put it into theAffectedRows
## The lesson record where id = 12 was deleted from the database
SQL Yoga USER GUIDE
- Home
- SQL Yoga Objects
- Database Objects
- Connection Objects
- SQL Query Objects
- SQL Record Objects
- Table Objects
- Table Object Behaviors
- Relationships
- Scopes
- Schema
- Working with User Search Strings
- SQL Query Template Objects
- Error Handling
- Migrating from SQL Yoga 1.x
- Integrating with the Levure Application Framework