C APIs provide basic data structures and functions to handle the interaction between the client and the server.
The basic methods for an application to interact with an OBServer by using OceanBase Connector/C include:
Call
mysql_library_init()to initialize the MySQL library.Call
mysql_init()to initialize a connection handle and callmysql_real_connect()to connect to the OBServer.Call
mysql_real_query()ormysql_query()to send SQL statements to the OBServer. Then, callmysql_store_result()ormysql_use_result()to process the results. Next, callmysql_free_result()to release the memory.Call
mysql_close()to disconnect from the OBServer.Call
mysql_library_end()to end the use oflibobclient.
For more information about APIs, see C API functions.
Example:
mysql_library_init(0, NULL, NULL);
MYSQL *mysql = mysql_init(NULL);
/* Use the CLIENT_MULTI_STATEMENTS option to connect to the server. */
if (mysql_real_connect (mysql, host_name, user_name, password****,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS) == NULL)
{
printf("mysql_real_connect() failed\n");
mysql_close(mysql);
mysql_library_end();
exit(1);
}
/* Execute multiple statements. */
status = mysql_query(mysql,
"DROP TABLE IF EXISTS tbl1;\
CREATE TABLE tbl1(id INT);\
INSERT INTO tbl1 VALUES(110);\
UPDATE tbl1 SET id=120 WHERE id=110;\
SELECT * FROM tbl1;\
DROP TABLE tbl1");
if (status)
{
printf("Could not execute statement(s)");
mysql_close(mysql);
mysql_library_end();
exit(0);
}
/* Process the result of each statement. */
do {
/* Check whether the current statement returns data. */
result = mysql_store_result(mysql);
if (result)
{
/* If yes, process the rows and release the result set. */
process_result_set(mysql, result);
mysql_free_result(result);
}
else /* No result set generated or an error occurred. */
{
if (mysql_field_count(mysql) == 0)
{
printf("%lld rows affected\n",
mysql_affected_rows(mysql));
}
else /* An error occurred. */
{
printf("Could not retrieve result set\n");
break;
}
}
/* Check whether more results are generated. The value -1 indicates no. The value 0 indicates yes, which means the loop continues. A value greater than 0 indicates that an error occurred. */
if ((status = mysql_next_result(mysql)) > 0)
printf("Could not execute statement\n");
} while (status == 0);
mysql_close(mysql);
mysql_library_end();