Getting Started with SQLAPI++ Your Hello World App

Welcome to SQLAPI++!

This tutorial introduces you to the essentials of SQLAPI++ by walking you through building a small application that makes a connection to database, inserts some data into a table, and fetches the data back.

Use side navigation for more in-depth explanations of different concepts and techniques, along with additional topics not covered by the tutorial.

Basic Application Template

#include <stdio.h>  // for printf
#include <SQLAPI.h> // main SQLAPI++ header

int main(int argc, char* argv[]) {
    SAConnection con;
    try {
        con.Connect("my_db", "my_user", "my_password", SA_Oracle_Client);
        printf("We are connected!\n");

        The rest of the tutorial goes here!
        printf("We are disconnected!\n");
    catch(SAException &x) {
        printf("%s\n", (const char*)x.ErrText());
    return 0;

This outline for the program demonstrates the following:

  • Include SQLAPI.h to get access to the library
  • To connect to database, allocate a SAConnection object and use SAConnection::Connect method to connect to your database provider using your credentials
  • Handle SAException exception for all database related problems including connectivity, database client and database server errors
  • You can optionally use SAConnection::Disconnect to disconnect from the server. If you don't, the library will implicitly do that for you when connection object is destructed

Insert Data into a Table

SACommand insert(&con, "INSERT INTO EMPLOYEES (NAME, AGE) VALUES (:1, :2)");

insert << "Tom Patt" << 30L;
insert << "Nick Barry" << 35L;

This snippet of code demonstrates the following:

  • To execute any SQL command on the server you create a SACommand object, passing it a valid connection object
  • SQLAPI++ supports parameter markers to bind input data to SQL commands. This example uses positional parameters, but the library also supports named parameters
  • To actually send the command to the server use SACommand::Execute method

Read Data from a Table


select << 30L;

while(select.FetchNext()) {
    SAString sName = select[1];
    long nAge = select[2];
    printf("Name: %s, age: %d \n", sName, nAge);

In this code snippet:

  • You use SACommand again, this time for SELECT statement
  • You use parameter binding, just like we did for INSERT command
  • Use SACommand::Execute to send your query to the server
  • SACommand::FetchNext method is used to fetch results set row by row
  • You access SELECT fields in the result set using field access operators. This example uses positional operators but the library also supports access by name
This is it - you just mastered the basics!

Next Steps

Explore Getting Started topics in greater depth:

Check out complete API documentation or FAQ section.