Wiki
Clone wikiTT's Persistent Objects for REALbasic / Example Project
The Example Project
If you download the project, you'll see that it contains two folders: One named "Example", which contains the example code, and one named "TTsPersistentObjects", containing the classes you can re-use in other projects.
This wiki page leads you through the example app.
Try out the Example App
The example shows a little database application in which you can manage a music library.
You can add albums, and you can add songs to each album.
Now run the project. It will greet you with this window, where you can edit the owner name as you like:
Click on the bottom left button:
Enter an album name.
Click on the bottom right button:
Enter a few song titles.
You'll end up with something like this:
Now you may quit the application. If you relaunch it, it'll come back with the data you had just entered.
The Database Contents
The project folder now contains a new file: Music-DB.rsd
This is a SQLite database file, as written by RB's REALSQLDatabase class.
You could now get a program to view the database, e.g. MesaSQLite or SQLVue (both are made with REAL Studio, BTW).
Here is the database schema of the Songs table as seen in SQLVue:
Contents of the Songs table:
Contents of the Albums table:
Contents of the Library table:
As you can see, all tables contain a value such as a Title, while two of them also contain references to another table (AlbumID and LibraryID).
For example, the song "Dear God" has an ID of 2 and means to refer to an album with ID 1. Now, if you look at the album contents, you'll find that the album with ID 1 is "Skylarking". And the album refers to a library entry of ID 1, which is the "Me" entry.
If you'd add another album and then add songs to that album, the new Album might get the ID 2, the new songs would get IDs 4 and up, and their AlbumID would be 2, identifying that new Album.
The names such as AlbumID are arbitrary, as you'll see further down. The only one that's fixed is "ID". That's used internally to uniquely identify any table entry.
The Data Classes
The supporting classes consist currently of the following:
The most important one is TTsPersistentObject. That's the base class from which your data classes need to be subclassed.
The example code has three such data classes which are all based on TTsPersistentObject:
The classes define the properties that we've already seen in the database:
"MusicLibrary" class
"MusicAlbum" class
"MusicSong" class
Note the red mark on some of the properties. That's an "A" - for Attribute.
This means that these properties have custom attributes set. To see them, right-click on the property and choose "Set Attributes" from the menu:
If you look at the attributes for one of the Title properties, you'll see this:
And the attribute for the AlbumID and LibraryID properties looks like this:
The Program Code
Explore the code yourself. Look into these places:
- Window1.Open -- sets everything up
- Window1.BuildAlbumList -- shows how to load all albums
- Window1.BuildSongList -- shows how to load only songs for one album
- Window1.addAlbumBut.Action -- creates and saves a new album
- Window1.albumList.CellAction -- updates the album title (after double click into list)
Updated