Support Functions

Our example includes three simple helper functions to support its work. The first initializes everything:

MySQL * connection, mysql;
  
int init_api(  ) {
    mysql_init(&mysql);
    connection = mysql_real_connect(&mysql, "localhost", "orausr",
                                    "orapw", "oradb", 0, 0, 0);
    if( connection == NULL ) {
        return -1;
    }
    else {
        return 0;
    }
}

This function does the MySQL initialization we covered earlier in the chapter. It specifically establishes a connection to the database. If any error occurs while making the connection, it returns -1.

The second support function is the opposite of the initialization function; it closes everything:

void close_api(  ) {
    if( connection != NULL ) {
        mysql_close(connection);
        connection = NULL;
    }
}

The last support function will be used many times in the example. It returns a message for the last error.

char *error;
  
char *get_error(  ) {
    return error;
}

Any time the API encounters an error condition, it sets the error variable to the appropriate error message. Applications calling this API therefore call this function to retrieve the last error message. You should always copy any error messages into storage managed by your application if you wish to save them for later use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset