Skip to content

Working with the Database

Luke Pring edited this page Mar 25, 2025 · 12 revisions

To access data stored in the database, the VMSDatabase class can be used.

The class was made to speed up development by simplifying database connection. It uses the Sqlite3 library.

Warning: Ensure you link libsqlite3 in the linker or the build will fail.

An instance of this class is already initialised at the top of the main.cpp file. (VMSDatabase VMSDatabase;)

Functions

Function Name Example Available
getData(table) VMSDatabase.getData("pets");
printTable(table) VMSDatabase.printTable("pets");
addRecord(table, data) VMSDatabase.addRecord("pets", {"Bob", "Pug", "3", "Has asthma", "1", "1"});
deleteRecord(table, id) VMSDatabase.deleteRecord("pets", 1)

Table parameter

The table parameter is made easy with a set of short options for each table. It is passed as a single word string.

Valid strings

  • pets - Pet Management
  • owners - Owner Management
  • appointments - Appointment Management
  • staff - Staff Management

getData

This function is used to retrieve data from the database.

The data is returned as a <vector>. A single value from the table can be referenced simply.

Example: std::cout << VMSDatabase.getData("pets")[0][1];

Explanation: Print the second value (Pet Name) from the first record in the Pet Management table.

Result: Bob

printTable

This function can be used to quickly display the contents of a database table to the user. It's not as well formatted as I'd like but it works.

Example: VMSDatabase.printTable("owners");

Explanation: Print a table of the data from Owner Management to the console.

Result: [table]

addRecord

This function can be used to add a record to any table. The two parameters are the table to change, and a vector of string values.

Example: VMSDatabase.addRecord("pets", {"Bob", "Pug", "3", "Has asthma", "1", "1"});

Explanation: Add a record to the Pet Management table with the values Bob, Pug, 3, Has asthma, 1 and 1.

Result: This record is added and saved to the database.

deleteRecord

This function can be used to remove a record from any table. The two parameters are the table to change, and an int ID of the record to delete.

Example: VMSDatabase.deleteRecord("owners", 5);

Explanation: Remove the owner record from the database with the OwnerID of 5.

Result: This record is deleted if existing. If not, a warning is logged to the console.

Clone this wiki locally