docs/content/stable/develop/tutorials/build-apps/c/cloud-ysql-c.md
The following tutorial shows a small C application that connects to a YugabyteDB cluster using the libpq driver and performs basic SQL operations. Use the application as a template to get started with YugabyteDB Aeon in C.
brew install libpq. You can download the PostgreSQL binaries and source from PostgreSQL Downloads.Clone the sample application to your computer:
git clone https://github.com/YugabyteDB-Samples/yugabyte-simple-c-app.git && cd yugabyte-simple-c-app
If your cluster is running on YugabyteDB Aeon, you need to modify the connection parameters so that the application can establish a connection to the YugabyteDB cluster. (You can skip this step if your cluster is running locally and listening on 127.0.0.1:5433.)
To do this:
Open the sample-app.c file.
Set the following configuration-related macros:
yugabyte).yugabyte and yugabyte). For YugabyteDB Aeon, use the credentials in the credentials file you downloaded.verify-full.Save the file.
Build the application with gcc or clang.
gcc sample-app.c -o sample-app -I<path-to-libpq>/libpq/include -L<path-to-libpq>/libpq/lib -lpq
Replace <path-to-libpq> with the path to the libpq installation; for example, /usr/local/opt.
Start the application.
$ ./sample-app
You should see output similar to the following:
>>>> Successfully connected to YugabyteDB!
>>>> Successfully created table DemoAccount.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 10000
name = John, age = 28, country = Canada, balance = 9000
>>>> Transferred 800 between accounts.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 9200
name = John, age = 28, country = Canada, balance = 9800
You have successfully executed a basic C application that works with YugabyteDB Aeon.
Open the sample-app.c file in the yugabyte-simple-c-app folder to review the methods.
The connect method establishes a connection with your cluster via the libpq driver.
PQinitSSL(1);
conn = PQconnectdb(CONN_STR);
if (PQstatus(conn) != CONNECTION_OK) {
printErrorAndExit(conn, NULL);
}
printf(">>>> Successfully connected to YugabyteDB!\n");
return conn;
The createDatabase method uses PostgreSQL-compliant DDL commands to create a sample database.
res = PQexec(conn, "DROP TABLE IF EXISTS DemoAccount");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}
res = PQexec(conn, "CREATE TABLE DemoAccount ( \
id int PRIMARY KEY, \
name varchar, \
age int, \
country varchar, \
balance int)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}
res = PQexec(conn, "INSERT INTO DemoAccount VALUES \
(1, 'Jessica', 28, 'USA', 10000), \
(2, 'John', 28, 'Canada', 9000)");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}
The selectAccounts method queries your distributed data using the SQL SELECT statement.
res = PQexec(conn, "SELECT name, age, country, balance FROM DemoAccount");
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
printErrorAndExit(conn, res);
}
for (i = 0; i < PQntuples(res); i++) {
printf("name = %s, age = %s, country = %s, balance = %s\n",
PQgetvalue(res, i, 0), PQgetvalue(res, i, 1), PQgetvalue(res, i, 2), PQgetvalue(res, i, 3));
}
The transferMoneyBetweenAccounts method updates your data consistently with distributed transactions.
res = PQexec(conn, "BEGIN TRANSACTION");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}
res = PQexec(conn, "UPDATE DemoAccount SET balance = balance - 800 WHERE name = \'Jessica\'");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}
res = PQexec(conn, "UPDATE DemoAccount SET balance = balance + 800 WHERE name = \'John\'");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}
res = PQexec(conn, "COMMIT");
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
printErrorAndExit(conn, res);
}