Xojo has its own built-in database engine. This tutorial shows how to get started with this type of database.
NOTE: Using databases requires some knowledge of SQL (Structured Query Language). A few very basic commands will be presented in this tutorial. If you intend to make productive use of Xojo’s database capabilities, at some point, you will need to take on the task of learning more about SQL. However, you don’t need to worry about it in order to follow this tutorial.
Databases organize and manage data. They can be designed for many different types of data. A database is made up of one or more tables. A table is made up of a group of fields. Fields are often referred to as columns because that’s how they look in a graphical representation of a table like the one below. This table has 3 fields or columns.
When data is entered into the fields of a table, rows of data are formed. These rows are also known as records.
Through the use of plugins, Xojo can deal with a number of popular database types. However, for this tutorial, we will be using the built-in Xojo database type.
Part 1 – Adding a database to the project and creating a table
Use the Xojo File menu item “Add Data Source” and choose the “New REAL Database…” submenu item. You will be asked to name your database and to choose the location where it should be created. Name it “SimpleDB” and save it in the same folder as your project.
Double-click on SimpleDB in the project window.
In the SimpleDB Tables window that comes up click the New Table button.
In the Table Editing window that appears enter “People” as the Table Name. Add fields as shown in the picture below. You need to specify a Field Name and a Field Type for each field you add. After you add the field called “ID” select it in the list and double-click it. In the window that comes up click the “Unique (Primary key)” checkbox. This just specifies that this field must always have a unique value in it. It is used by the table to keep track of its records.
A database can have multiple tables but our database will have only the “People” table.
Add a module to the project and add a property to the module called “db as database”. This is a global property that we can use anywhere in our project to refer to our SimpleDB database.
In the “app” class’ Open event put this code:
db = SimpleDB//open the database
if db = nil then
msgbox “SimpleDB not found.”
This assigns SimpleDB to the global property db when the application launches so that we can refer to SimpleDB whenever we need to.
Part 2 – Creating the interface
Add controls to a window as shown in this picture. Using the properties window set the window name to “dataBrowserWindow”.
Name the fields as follows:
The heart of this window is the DataControl object. Simply by setting some of its properties we can get it to navigate the records we will be creating in our database. Amazingly, no coding will be required to accomplish this.