Build a C application

Prerequisites

The tutorial assumes that you have:

  • installed YugabyteDB, and created a universe. If not, please follow these steps in the Quick Start guide.
  • have a 32-bit (x86) or 64-bit (x64) architecture machine.
  • have gcc 4.1.2+, clang 3.4+ installed.

Install the C driver (libpq)

The C driver is already available as part of the YugabyteDB installation. You can use it by setting the LD_LIBRARY_PATH as follows :-

  1. $ export LD_LIBRARY_PATH=<yugabyte-install-dir>/postgres/lib

Alternatively, you can download the PostgreSQL binaries or build the driver from source as documented here.

Working example

Sample C code

Create a file ybsql_hello_world.c and copy the contents below:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "libpq-fe.h"
  4. int
  5. main(int argc, char **argv)
  6. {
  7. const char *conninfo;
  8. PGconn *conn;
  9. PGresult *res;
  10. int nFields;
  11. int i, j;
  12. /* connection string */
  13. conninfo = "host=127.0.0.1 port=5433 dbname=yugabyte user=yugabyte password=yugabyte";
  14. /* Make a connection to the database */
  15. conn = PQconnectdb(conninfo);
  16. /* Check to see that the backend connection was successfully made */
  17. if (PQstatus(conn) != CONNECTION_OK)
  18. {
  19. fprintf(stderr, "Connection to database failed: %s",
  20. PQerrorMessage(conn));
  21. PQfinish(conn);
  22. exit(1);
  23. }
  24. /* Create table */
  25. res = PQexec(conn, "CREATE TABLE employee (id int PRIMARY KEY, \
  26. name varchar, age int, \
  27. language varchar)");
  28. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  29. {
  30. fprintf(stderr, "CREATE TABLE failed: %s", PQerrorMessage(conn));
  31. PQclear(res);
  32. PQfinish(conn);
  33. exit(1);
  34. }
  35. PQclear(res);
  36. printf("Created table employee\n");
  37. /* Insert a row */
  38. res = PQexec(conn, "INSERT INTO employee (id, name, age, language) \
  39. VALUES (1, 'John', 35, 'C')");
  40. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  41. {
  42. fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn));
  43. PQclear(res);
  44. PQfinish(conn);
  45. exit(1);
  46. }
  47. PQclear(res);
  48. printf("Inserted data (1, 'John', 35, 'C')\n");
  49. /* Query the row */
  50. res = PQexec(conn, "SELECT name, age, language FROM employee WHERE id = 1");
  51. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  52. {
  53. fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
  54. PQclear(res);
  55. PQfinish(conn);
  56. exit(1);
  57. }
  58. /* print out the rows */
  59. nFields = PQnfields(res);
  60. for (i = 0; i < PQntuples(res); i++)
  61. {
  62. printf("Query returned: ");
  63. for (j = 0; j < nFields; j++)
  64. printf("%s ", PQgetvalue(res, i, j));
  65. printf("\n");
  66. }
  67. PQclear(res);
  68. /* close the connection to the database and cleanup */
  69. PQfinish(conn);
  70. return 0;
  71. }

Run the application

You can compile the file using gcc or clang.For gcc, you can use:

  1. $ gcc ybsql_hello_world.c -lpq -I<yugabyte-install-dir>/postgres/include -o ybsql_hello_world

Run with:

  1. $ ./ybsql_hello_world

You should see the following output:

  1. Created table employee
  2. Inserted data (1, 'John', 35, 'C')
  3. Query returned: John 35 C