Build Applications
From;




gcc -o example example.c $(mariadb_config --include --libs)

MariaDB Connector/C includes several header files. In some cases, developers
might find it useful to inspect the MariaDB Connector/C header files to view
the definitions of structures, functions, and constants.

The header files:


C applications developed using MariaDB Connector/C must include the 
mysql.h header file.



Connect Connect with MariaDB Connector/C Topics on this page: Overview Connection Info Code Example: Connect Overview MariaDB Connector/C enables C and C++ applications to establish client connections to MariaDB database products over TLS. Additional information on MariaDB Connector/C is available in the MariaDB Knowledge Base. Connection Info The connection is configured via the information that is initially acquired from the SkySQL Portal pages:
Function Option/Argument Where to find it
mysql_optionsv() MYSQL_OPT_SSL_CA option The path to the skysql_chain.pem file containing the "Certificate Authority Chain"
  • Download For GCP services
  • Download For AWS services
mysql_real_connect() host argument The fully Qualified Domain Name in the Service Details view
mysql_real_connect() user argument The desired username, which might be the default username in the Service Credentials view
mysql_real_connect() passwd argument The user's password, which might be the default password in the Service Credentials view if it was not yet customized
mysql_real_connect() port argument The Read-Write Port or Read-Only Port in the Service Details view

Code Example: Connect The following code demonstrates how to use MariaDB Connector/C to connect to MariaDB database products. This example uses the example database and user account: /*H***************************************************** * *******************************************************/ #include <stdio.h> #include <stdlib.h> #include <mysql.h> /*H***************************************************** * *******************************************************/ int main ( int argc, char *argv[] ) { // Initialize Connection MYSQL *conn; if( !(conn = mysql_init(0))) { fprintf(stderr, "unable to initialize connection struct\n"); exit(1); } char* cafile = "/path/to/skysql_chain.pem"; // Configure the TLS Certificate Authority. This may vary by hosting provider! mysql_optionsv( conn, MYSQL_OPT_SSL_CA, cafile ); // Connect to the database if( !mysql_real_connect( conn, // Connection "example.skysql.net", // Host "db_user", // User account "db_user_password", // User password "test", // Default database 5009, // Port number NULL, // Path to socket file 0 // Additional options )) { // Report the failed-connection error & close the handle fprintf( stderr, "Error connecting to Server: %s\n", mysql_error(conn)); mysql_close( conn ); exit( 1 ); } // Use Connection // ... // Close Connection mysql_close( conn ); return( 0 ); }