All pages
Powered by GitBook
1 of 77

MariaDB Connector/C API Functions

Explore API functions for MariaDB Connector/C. This section provides detailed documentation on functions for connecting, querying, and managing data, enabling robust C applications for MariaDB.

mariadb_cancel

Syntax

int mariadb_cancel(MYSQL * mysql);

mysql - mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Immediately aborts a connection by making all subsequent read/write operations fail.mariadb_cancel() does not invalidate memory used for mysql structure, nor close any communication channels. To free the memory, mysql_close() must be called.mariadb_cancel() is useful to break long queries in situations where sending KILL is not possible.

mariadb_cancel() was added in Connector/C 3.0

mariadb_get_infov

Syntax

int mariadb_get_infov(MYSQL * mysql,
                      enum mariadb_value value,
                      void * arg,
                      ...);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect(). For general information which is not bound to connection this parameter might be null.

  • value - the type of value you want to retrieve. See description below.

  • arg - pointer to a variable for storing value of the specified option.

  • ... - variable argument list

Description

Retrieves generic or connection specific information.

Returns zero on success, non zero if an error occurred (invalid option),

This function was added in MariaDB Connector/C 3.0,

Value types

Generic information

For these information types parameter mysql needs to be set to NULL.

  • MARIADB_CHARSET_NAME: Retrieves the charset information for a character set by it's literal representation.Parameter type: const MARIADB_CHARSET_INFO*.

  • MARIADB_CLIENT_ERRORS: Retrieve array of client errors. This can be used in plugins to set global error messages (which are not exported by MariaDB Connector/C).Parameter type: const char **.

  • MARIADB_CLIENT_VERSION: The client version in literal representation.Parameter type: const char *.

  • MARIADB_CLIENT_VERSION_ID: The client version in numeric format.Parameter type: unsigned int.

  • MARIADB_MAX_ALLOWED_PACKET: Retrieves value of maximum allowed packet size.Parameter type: size_t

  • MARIADB_NET_BUFFER_LENGTH: Retrieves the length of net buffer.Parameter type: size_t

  • MARIADB_TLS_LIBRARY: The TLS library MariaDB Connector/C is compiled against.Parameter type: const char *.

Connection related information

  • MARIADB_CONNECTION_ASYNC_TIMEOUT: Retrieves the timeout for non blocking calls in seconds.Parameter type: unsigned int.

  • MARIADB_CONNECTION_ASYNC_TIMEOUT_MS: Retrieves the timeout for non blocking calls in milliseconds.Parameter type: unsigned int.

  • MARIADB_CONNECTION_MARIADB_CHARSET_INFO: Retrieves character set information for given connection. Parameter type: const MY_CHARSET_INFO *.

  • MARIADB_CONNECTION_CLIENT_CAPABILITIES: Returns the capability flags of the client.Parameter type: unsigned long.

  • MARIADB_CONNECTION_ERROR: Retrieves error message for last used command. Parameter type: const char *.

  • MARIADB_CONNECTION_ERROR_ID: Retrieves error number for last used command. Parameter type: unsigned int.

  • MARIADB_CONNECTION_EXTENDED_SERVER_CAPABILITIES: Returns the extended capability flags of the connected MariaDB server.Parameter type: unsigned long.

  • MARIADB_CONNECTION_HOST: Retrieves connection's host name. Parameter type: const char *.

  • MARIADB_CONNECTION_INFO: Retrieves generic info for last used command.Parameter type: const char *.

  • MARIADB_CONNECTION_PORT: Retrieves the port number of server host.Parameter type: unsigned int.

  • MARIADB_CONNECTION_PROTOCOL_VERSION_ID: Retrieves the protocol version number.Parameter type: unsigned int.

  • MARIADB_CONNECTION_PVIO_TYPE: Retrives the pvio plugin used for specified connection.Parameter type: unsigned int.

  • MARIADB_CONNECTION_SCHEMA: Retrieves the current schema.Parameter type: const char*.

  • MARIADB_CONNECTION_SERVER_CAPABILITIES: Returns the capability flags of the connected server.Parameter type: unsigned long.

  • MARIADB_CONNECTION_SERVER_STATUS: Returns server status after last operation. A list of possible flags can be found in the description OK packet.Parameter type: unsigned int.

  • MARIADB_CONNECTION_SERVER_TYPE: Retrieves the type of the server.Parameter type: const char*.

  • MARIADB_CONNECTION_SERVER_VERSION: Retrieves the server version in literal format.Parameter type: const char *.

  • MARIADB_CONNECTION_SERVER_VERSION_ID: Retrieves the server version in numeric format.Parameter type: unsigned int.

  • MARIADB_CONNECTION_SOCKET: Retrieves the handle (socket) for given connection.Parameter type: my_socket.

  • MARIADB_CONNECTION_SQLSTATE: Retrieves current sqlstate information for last used command. Parameter type: const char *.

  • MARIADB_CONNECTION_SSL_CIPHER: Retrieves the TLS cipher in use.Parameter type: const char *.

  • MARIADB_CONNECTION_TLS_VERSION: Retrieves the TLS protocol version used in literal format.Parameter type: char *.

  • MARIADB_CONNECTION_TLS_VERSION_ID: Retrieves the TLS protocol version used in numeric format.Parameter type: unsigned int.

  • MARIADB_CONNECTION_UNIX_SOCKET: Retrieves the file name of the unix socketParameter type: const char *.

  • MARIADB_CONNECTION_USER: Retrieves connection's user name.Parameter type: const char *.

Examples

/* get server port for current connection */
unsigned int port;
mariadb_get_infov(mysql, MARIADB_CONNECTION_PORT, void *)&port);
/* get user name for current connection */
const char *user;
mariadb_get_infov(mysql, MARIADB_CONNECTION_USER, (void *)&user);

See also

  • mysql_get_optionv()

mariadb_reconnect

Syntax

my_bool  mariadb_reconnect(MYSQL * mysql)
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

mariadb_reconnect() tries to reconnect to a server in case the connection died due to timeout or other errors. It uses the same credentials which were specified in mysql_real_connect().

The function will return 0 on sucess.

The function will return an error, if the option MYSQL_OPT_RECONNECT wasn't specified before.

This function was added in Connector/C 3.0.

See also

  • mysql_real_connect()

  • mysql_options()

mysql_affected_rows

Syntax

my_ulonglong mysql_affected_rows(MYSQL * mysql);

mysql is a connection identifier, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the number of affected rows by the last operation associated with mysql, if the operation was an "upsert" (INSERT, UPDATE, DELETE or REPLACE) statement, or UINT64_MAX (0xffffffffffffffff) if the last query failed.

When using UPDATE, MariaDB will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.

The REPLACE statement first deletes the record with the same primary key and then inserts the new record. This function returns the number of deleted records in addition to the number of inserted records.

See also

  • mysql_num_rows()

mysql_autocommit

Syntax

my_bool mysql_autocommit(MYSQL * mysql, my_bool auto_mode);
  • mysql - a mysql handle, identifier, which was previously allocated by mysql_init() or mysql_real_connect().

  • auto_mode - whether to turn autocommit on or not.

Description

Toggles autocommit mode on or off for the current database connection. Autocommit mode will be set if mode=1 or unset if mode=0. Returns zero on success, or nonzero if an error occurred. Parameters

Autocommit mode only affects operations on transactional table types. To determine the current state of autocommit mode use the SQL command SELECT @@autocommit. Be aware: the mysql_rollback() function will not work if autocommit mode is switched on.

Turning off autocommit in sql

SET AUTOCOMMIT=0;

mysql_change_user

Syntax

my_bool mysql_change_user(MYSQL * mysql,
                          const char * user,
                          const char * passwd,
                          const char * db);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • user - the user name for server authentication

  • passwd - the password for server authentication

  • db - the default database. If desired, the NULL value may be passed resulting in only changing the user and not selecting a database. To select a database in this case use the mysql_select_db() function.

Description

Changes the user and default database of the current connection.

In order to successfully change users a valid username and password parameters must be provided and that user must have sufficient permissions to access the desired database. If for any reason authorization fails, the current user authentication will remain.

Returns zero on success, nonzero if an error occured.

mysql_change_user will always cause the current database connection to behave as if was a completely new database connection, regardless of if the operation was completed successfully. This reset includes performing a rollback on any active transactions, closing all temporary tables, and unlocking all locked tables.

mysql_character_set_name

Syntax

const char * mysql_character_set_name(MYSQL * mysql);

mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the default client character set for the specified connection.

mysql_close

Syntax

void mysql_close(MYSQL * mysql);

mysql - mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Closes a previously opened connection.

mysql_commit

Syntax

my_bool mysql_commit(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Commits the current transaction for the specified database connection. Returns zero on success, nonzero if an error occurred.

Executing mysql_commit() will not affected the behaviour of autocommit. This means, any update or insert statements following mysql_commit() will be rolled back when the connection gets closed.

See also

  • mysql_rollback()

mysql_data_seek

Syntax

void mysql_data_seek(MYSQL_RES * result,
                     my_ulonglong offset);
  • result - a result set identifier returned by mysql_store_result().

  • offset - the field offset. Must be between zero and the total number of rows minus one (0..mysql_num_rows - 1).

Description

The mysql_data_seek() function seeks to an arbitrary function result pointer specified by the offset in the result set. Returns zero on success, nonzero if an error occurred.

This function can only be used with buffered result sets obtained from the use of the mysql_store_result function.

mysql_debug

Syntax

void mysql_debug(const char * debug);
  • debug - a string representing the debug operation to perform. See description below.

Description

Enables debug output for development and debug purposes by using Fred Fish's DBUG library. For using this function the mariadb-client library must be compiled with debug support.

Almost all MariaDB binaries use the DBUG library and one can get a trace of the program execution by using the --debug command line option with the binary. This will only work if the binary is compiled for debugging (compiler option -DDBUG_ON).

Returns void.

The debug control string is a sequence of colon separated fields as follows:

field_1:field_2:field_n

Each field consists of a mandatory flag character followed by an optional "," and comma separated list of modifiers:

flag[,modifier,modifier,...,modifier]

The currently recognized flag characters are:

Option
Description

d

Enable output from DBUG_ macros for the current state. May be followed by a list of keywords which selects output only for the DBUG macros with that keyword. A null list of keywords implies output for all macros.

D

Delay after each debugger output line. The argument is the number of tenths of seconds to delay, subject to machine capabilities. I.E. -#D,20 is delay two seconds.

f

Limit debugging and/or tracing, and profiling to the list of named functions. Note that a null list will disable all functions. The appropriate "d" or "t" flags must still be given, this flag only limits their actions if they are enabled.

F

Identify the source file name for each line of debug or trace output.

i

Identify the process with the pid for each line of debug or trace output.

g

Enable profiling. Create a file called 'dbugmon.out' containing information that can be used to profile the program. May be followed by a list of keywords that select profiling only for the functions in that list. A null list implies that all functions are considered.

L

Identify the source file line number for each line of debug or trace output.

n

Print the current function nesting depth for each line of debug or trace output.

N

Number each line of dbug output.

o

Redirect the debugger output stream to the specified file. The default output is stderr.

O

As o but the file is really flushed between each write. When needed the file is closed and reopened between each write.

a

Like o, but opens for append.

A

Like O, but opens for append.

p

Limit debugger actions to specified processes. A process must be identified with the DBUG_PROCESS macro and match one in the list for debugger actions to occur.

P

Print the current process name for each line of debug or trace output.

r

When pushing a new state, do not inherit the previous state's function nesting level. Useful when the output is to start at the left margin.

S

Do function _sanity(file,line) at each debugged function until _sanity() returns something that differs from 0. (Mostly used with safemalloc)

t

Enable function call/exit trace lines. May be followed by a list (containing only one modifier) giving a numeric maximum trace level, beyond which no output will occur for either debugging or tracing macros. The default is a compile time option.

Instead of using the mysql_debug() function you also can set the environment variable MYSQL_DEBUG\

Enabling generation of debug information slows down the overall performance and generates huge files. In case you need debug information only for special places you can disable the generation of debug information by using mysql_debug_end().

See also

  • mysql_debug_end()

  • mysql_dump_debug_info()

mysql_dump_debug_info

Syntax

int mysql_dump_debug_info(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

This function is designed to be executed by an user with the SUPER privilege and is used to dump server status information into the log for the MariaDB Server relating to the connection.

Returns zero on success, nonzero if an error occurred.

The server status information will be dumped into the error log file, which can usually be found in the data directory of your server installation.

See also

  • mysql_debug()

  • mysql_debug_end()

mysql_errno

Syntax

unsigned int mysql_errno(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the last error code for the most recent function call that can succeed or fail. Zero means no error occurred.

Client error messages are listed in errmsg.h header file, server error messages are listed in mysqld_error.h header file of the server source distribution.

See also

  • mysql_error()

  • mysql_sqlstate()

mysql_error

Syntax

const char * mysql_error(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the last error message for the most recent function call that can succeed or fail. If no error occurred an empty string is returned.

See also

  • mysql_errno()

  • mysql_sqlstate().

mysql_escape_string

Syntax

unsigned long mysql_escape_string(char * to,
                                  const char * from,
                                  unsigned long);

Description

Escapes a string using the default character set.

This function is deprecated and will be discontinued. Please use mysql_real_escape_string() instead.

See also

  • mysql_real_escape_string()

mysql_fetch_field

Syntax

MYSQL_FIELD * mysql_fetch_field(MYSQL_RES * result);
  • result - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

Returns the definition of one column of a result set as a pointer to a MYSQL_FIELD structure. Call this function repeatedly to retrieve information about all columns in the result set.

The field order will be reset if you execute a new SELECT query.

In case only information for a specific field is required the field can be selected by using the mysql_field_seek() function or obtained by mysql_fetch_field_direct() function.

See also

  • mysql_field_seek()

  • mysql_field_tell()

  • mysql_fetch_field_direct()

  • mysql_store_result()

  • mysql_use_result()

mysql_fetch_field_direct

Syntax

MYSQL_FIELD * mysql_fetch_field_direct(MYSQL_RES * res,
                                       unsigned int fieldnr);
  • res - a result set identifier returned by mysql_store_result() or mysql_use_result().

  • fieldnr - the field number. This value must be within the range from 0 to number of fields - 1

Description

Returns a pointer to a MYSQL_FIELD structure which contains field information from the specified result set.

The total number of fields can be obtained by mysql_field_count()

See also

  • mysql_fetch_field()

  • mysql_field_count()

mysql_fetch_fields

Syntax

MYSQL_FIELD * mysql_fetch_fields(MYSQL_RES * res);
  • res - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

This function serves an identical purpose to the mysql_fetch_field() function with the single difference that instead of returning one field at a time for each field, the fields are returned as an array. Each field contains the definition for a column of the result set.

The total number of fields can be obtained by mysql_field_count().

See also

  • mysql_fetch_field()

  • mysql_fetch_field_direct()

  • mysql_field_count()

mysql_fetch_lengths

Syntax

unsigned long * mysql_fetch_lengths(MYSQL_RES * result);
  • result - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

The mysql_fetch_lengths() function returns an array containing the lengths of every column of the current row within the result set (not including terminating zero character) or NULL if an error occurred.

mysql_fetch_lengths() is valid only for the current row of the result set. It returns NULL if you call it before calling mysql_fetch_row() or after retrieving all rows in the result.

See also

  • mysql_fetch_row()

mysql_fetch_row

Syntax

MYSQL_ROW mysql_fetch_row(MYSQL_RES * result);
  • result - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

Fetches one row of data from the result set and returns it as an array of char pointers (MYSQL_ROW), where each column is stored in an offset starting from 0 (zero). Each subsequent call to this function will return the next row within the result set, or NULL if there are no more rows.

If a column contains a NULL value the corresponding char pointer will be set to NULL.

Memory associated to MYSQL_ROW will be freed when calling mysql_free_result() function.

See also

  • mysql_use_result()

  • mysql_store_result()

mysql_field_count

Syntax

unsigned int mysql_field_count(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the number of columns for the most recent query on the connection represented by the link parameter as an unsigned integer. This function can be useful when using the mysql_store_result() function to determine if the query should have produced a non-empty result set or not without knowing the nature of the query.

The mysql_field_count() function should be used to determine if there is a result set available.

See also

  • mysql_store_result()

  • mysql_use_result()

mysql_field_seek

Syntax

MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES * result,
                                    MYSQL_FIELD_OFFSET offset);
  • result - a result set identifier returned by mysql_store_result() or mysql_use_result().

  • offset - the field number. This number must be in the range from 0..number of fields - 1.

Description

Sets the field cursor to the given offset. The next call to mysql_fetch_field() will retrieve the field definition of the column associated with that offset.

Returns the previous value of the field cursor.

The number of fields can be obtained from mysql_field_count().

To move the field cursor to the first field offset parameter should be null.

See also

  • mysql_field_tell()

mysql_field_tell

Syntax

MYSQL_FIELD_OFFSET mysql_field_tell(MYSQL_RES * result);
  • result - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

Return the offset of the field cursor used for the last mysql_fetch_field() call. This value can be used as a parameter for the function mysql_field_seek().

Returns the current offset of the field cursor

See also

  • mysql_field_seek()

mysql_free_result

Syntax

void mysql_free_result(MYSQL_RES * result);
  • result - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

Frees the memory associated with a result set. Returns void.

You should always free your result set with mysql_free_result() as soon it's not needed anymore

Row values obtained by a prior mysql_fetch_row() call will become invalid after calling mysql_free_result().

See also

  • mysql_store_result()

  • mysql_use_result()

mysql_get_character_set_info

Syntax

void mysql_get_character_set_info(MYSQL * mysql,
                                  MY_CHARSET_INFO * charset);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • charset - a pointer to a MY_CHARSET_INFO structure, in which the information will be copied.

Description

Returns information about the current default character set for the specified connection.

A complete list of supported character sets in the client library is listed in the function description for mysql_set_character_set_info().

mysql_get_client_info

Syntax

const char * mysql_get_client_info(void );

Description

Returns a string representing the client library version

To obtain the numeric value of the client library version use mysql_get_client_version().

See also

  • mysql_get_client_version()

  • mysql_get_host_info()

  • mysql_get_proto_info()

mysql_get_client_version

Syntax

unsigned long mysql_get_client_version(void);

Description

Returns a number representing the client library version.

To obtain a string containing the client library version use the mysql_get_client_info() function.

See also

  • mysql_get_client_info()

mysql_get_host_info

Syntax

const char * mysql_get_host_info(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Describes the type of connection in use for the connection, including the server host name. Returns a string, or NULL if the connection is not valid.

See also

  • mysql_get_server_version()

mysql_get_optionv

Syntax

int mysql_get_optionv(MYSQL * mysql,
                      enum mysql_option,
                      void * arg,
                      ...);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • mysql_option - the option you want to retrieve. See description below.

  • arg - pointer to a variable for storing value of the specified option.

  • ... - variable argument list

Description

Retrieves the value for a given option which was previously set by mysql_optionsv.

Returns zero on success, non zero if an error occurred (invalid option).

This function was added in MariaDB Connector/C 3.0.0.

Options

Boolean values (my_bool)

  • MYSQL_OPT_COMPRESS

  • MYSQL_OPT_NAMED_PIPE

  • MYSQL_OPT_RECONNECT

  • MYSQL_REPORT_DATA_TRUNCATION

  • MYSQL_OPT_NONBLOCK

  • MYSQL_OPT_SSL_VERIFY_SERVER_CERT

  • MARIADB_OPT_CONNECTION_READ_ONLY

  • MYSQL_SECURE_AUTH

Integer values

  • MYSQL_OPT_CONNECT_TIMEOUT

  • MYSQL_OPT_READ_TIMEOUT

  • MYSQL_OPT_WRITE_TIMEOUT

  • MYSQL_OPT_LOCAL_INFILE

  • MYSQL_OPT_PROTOCOL

Character values

  • MYSQL_INIT_COMMAND

  • MYSQL_READ_DEFAULT_FILE

  • MYSQL_READ_DEFAULT_GROUP

  • MYSQL_SET_CHARSET_NAME

  • MYSQL_PLUGIN_DIR

  • MYSQL_OPT_SSL_KEY

  • MYSQL_OPT_SSL_CERT

  • MYSQL_OPT_SSL_CA

  • MYSQL_OPT_SSL_CAPATH

  • MYSQL_OPT_SSL_CRL

  • MYSQL_OPT_SSL_CRLPATH

  • MYSQL_OPT_SSL_CIPHER

  • MARIADB_OPT_SSL_FP

  • MARIADB_OPT_SSL_FPLIST

  • MARIADB_OPT_SSL_PASSPHRASE

  • MYSQL_DEFAULT_AUTH

  • MYSQL_OPT_BIND

  • MARIADB_OPT_CONNECTION_HANDLER

Misc

  • MYSQL_PROGRESS_CALLBACK: requires a function pointer *(const MYSQL *, uint, uint, double, const char *, uint))arg)

  • MYSQL_CONNECT_ATTRS: this option requires 5 parameters:

/* get number of connection attributes */
int i, elements= 0;
char **key, **value;

mysql_get_optionv(mysql, MYSQL_CONNECT_ATTRS, NULL, NULL, (void *)&elements);
key= (char **)malloc(sizeof(char *) * elements);
val= (char **)malloc(sizeof(char *) * elements);
mysql_get_optionv(mysql, MYSQL_OPT_CONNECT_ATTRS, &key, &val, &elements);
for (i=0; i < elements; i++)
  printf("key: %s value: %s", key[i], val[i]);
  • MARIADB_OPT_USERDATA: retrieves userdata for a given key.

const char *ssh_user;
mysql_get_optionv(mysql, MARIADB_OPT_USERDATA, "ssh_user", (void *)ssh_user);

See also

  • mysql_optionsv()

mysql_get_proto_info

Syntax

unsigned int mysql_get_proto_info(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the protocol version number for the specified connection

The client library doesn't support protocol version 9 and prior.

See also

  • mysql_get_host_info()

mysql_get_server_info

Syntax

const char * mysql_get_server_info(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the server version or NULL on failure.

To obtain the numeric server version please use mysql_get_server_version().

See also

  • mysql_get_server_version()

  • mysql_get_client_info()

mysql_get_server_version

Syntax

unsigned long mysql_get_server_version(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns an integer representing the version of connected server.

The form of the version number is VERSION_MAJOR * 10000 + VERSION_MINOR * 100 + VERSION_PATCH

See also

  • mysql_get_server_info()

mysql_get_ssl_cipher

Syntax

const char *mysql_get_ssl_cipher(MYSQL *mysql)
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the name of the currently used cipher of the TLS connection, or NULL for non TLS connections.

See also

  • mysql_ssl_set()

mysql_hex_string

Syntax

unsigned long mysql_hex_string(char * to,
                               const char * from,
                               unsigned long len);
  • to - result buffer

  • from - the string which will be encoded

  • len - length of the string (from)

Description

This function is used to create a hexadecimal string which can be used in SQL statements. e.g. INSERT INTO my_blob VALUES(X'A0E1CD').

Returns the length of the encoded string without the trailing null character.

The size of the buffer for the encoded string must be 2 * length + 1.

The encoded string does not contain a leading X'.

See also

  • mysql_real_escape_string()

mysql_info

Syntax

const char * mysql_info(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

The mysql_info() function returns a string providing information about the last query executed. The nature of this string is provided below:

Table 1. Possible mysql_info return values

Query type
Example result string

INSERT INTO...SELECT...

Records: 100 Duplicates: 0 Warnings: 0

INSERT INTO...VALUES (...),(...),(...)

Records: 3 Duplicates: 0 Warnings: 0

LOAD DATA INFILE ...

Records: 1 Deleted: 0 Skipped: 0 Warnings: 0

ALTER TABLE ...

Records: 3 Duplicates: 0 Warnings: 0

UPDATE ...

Rows matched: 40 Changed: 40 Warnings: 0

Queries which do not fall into one of the preceding formats are not supported (e.g. SELECT ...). In these situations mysql_info() will return an empty string.

See also

  • mysql_affected_rows()

  • mysql_warning_count()

mysql_init

Syntax

MYSQL * mysql_init(MYSQL * mysql);

mysql - a pointer to MYSQL or NULL. In case of passing a NULL pointer mysql_init() will allocate memory and return a pointer to a MYSQL structure.

Description

Prepares and initializes a MYSQL structure to be used with mysql_real_connect().

If mysql_thread_init() was not called before, mysql_init() will also initialize the thread subsystem for the current thread.

Members of the MYSQL structure are not intended for application use.

Any subsequent calls to any mysql function (except mysql_options()) will fail until mysql_real_connect() was called.

Memory allocated by mysql_init() must be freed with mysql_close().

See also

  • mysql_real_connect()

  • mysql_options()

  • mysql_thread_init()

  • mysql_close()

mysql_insert_id

Syntax

my_ulonglong mysql_insert_id(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

The mysql_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute or the value for the last usage of LAST_INSERT_ID(expr). If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute and LAST_INSERT_ID was not used, this function will return zero.

When performing a multi insert statement, mysql_insert_id() will return the value of the first row.

See also

  • AUTO INCREMENT

  • mysql_stmt_insert_id()

  • LAST_INSERT_ID()

mysql_kill

Syntax

int mysql_kill(MYSQL * mysql,
               unsigned long);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • long - process id

Description

This function is used to ask the server to kill a MariaDB thread specified by the processid parameter. This value must be retrieved by SHOW PROCESSLIST. If trying to kill the own connection mysql_thread_id() should be used.

Returns 0 on success, otherwise nonzero.

To stop a running command without killing the connection use KILL QUERY. The mysql_kill() function only kills a connection, it doesn't free any memory - this must be done explicitly by calling mysql_close().

See also

  • mysql_thread_id()

  • mysql_close()

  • mariadb_cancel()

mysql_library_end

Syntax

void mysql_library_end(void)

Description

Call when finished using the library, such as after disconnecting from the server. In an embedded server application, the embedded server is shut down and cleaned up. For a client program, only cleans up by performing memory management tasks.

mysql_server_end() is an alias.

See also

  • mysql_library_init()

mysql_library_init

Syntax

int mysql_library_init(int argc, char **argv, char **groups)

Description

Call to initialize the library before calling other functions, both for embedded servers and regular clients. If used on an embedded server, the server is started and subsystems initialized. Returns zero for success, or nonzero if an error occurred.

Call mysql_library_end() to clean up after completion.

mysql_server_init() is an alias.

See also

  • mysql_library_end()

mysql_more_results

Syntax

my_bool mysql_more_results(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Indicates if one or more result sets are available from a previous call to mysql_real_query(). Returns 1 if more result sets are available, otherwise zero. .

The function mysql_set_server_option() enables or disables multi statement support.

See also

  • mysql_real_query()

  • mysql_use_result()

  • mysql_store_result()

  • mysql_next_result()

mysql_next_result

Syntax

int mysql_next_result(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Prepares next result set from a previous call to mysql_real_query() which can be retrieved by mysql_store_result() or mysql_use_result(). Returns zero on success, nonzero if an error occurred.

If a multi query contains errors the return value of mysql_errno/error() might change and there will be no result set available.

See also

  • mysql_real_query()

  • mysql_store_result()

  • mysql_use_result()

  • mysql_more_results()

mysql_num_fields

Syntax

unsigned int mysql_num_fields(MYSQL_RES * );
  • MYSQL RES * - A result set identifier returned by mysql_store_result() or mysql_use_result().

Description

Returns number of fields in a specified result set.

See also

  • mysql_fetch_field()

mysql_num_rows

Syntax

my_ulonglong mysql_num_rows(MYSQL_RES * );
  • MYSQL_RES - a result set identifier returned by mysql_store_result() or mysql_use_result().

Description

Returns number of rows in a result set.

The behavior of mysql_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysql_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved.

See also

  • mysql_affected_rows()

  • mysql_use_result()

  • mysql_store_result()

mysql_options

Syntax

int mysql_options(MYSQL * mysql,
                  enum mysql_option,
                  const void * arg);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • mysql_option - the option you want to set. See description below.

  • arg - the value for the option.

Description

Used to set extra connect options and affect behavior for a connection. This function may be called multiple times to set several options. mysql_options() should be called after mysql_init() and before mysql_real_connect().

Returns zero on success, non zero if an error occurred (invalid option or value).

This function is deprecated as of MariaDB Connector/C 3.0 and will be removed in future releases. It's preferable to use mysql_optionsv.

Options

See mysql_optionsv.

See also

  • mysql_init()

  • mysql_optionsv

  • mysql_real_connect()

mysql_optionsv

Syntax

int mysql_optionsv(MYSQL * mysql,
                   enum mysql_option,
                   const void * arg,
                   ...);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • mysql_option - the option you want to set. See description below.

  • arg - the value for the option.

  • ... - variable argument list

Description

Used to set extra connect options and affect behavior for a connection. This function may be called multiple times to set several options. mysql_optionsv() should be called after mysql_init().

Some of these options can also be set in option files, such as my.cnf.

Returns

Returns zero on success, non zero if an error occurred (invalid option or value).

Options

  • MYSQL_INIT_COMMAND: Command(s) which will be executed when connecting and reconnecting to the server.

    mysql_optionsv(mysql, MYSQL_INIT_COMMAND, (void *)"CREATE TABLE ...");
  • MYSQL_OPT_CONNECT_TIMEOUT: Connect timeout in seconds. This value will be passed as an unsigned int parameter.

    unsigned int timeout= 5;
    mysql_optionsv(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (void *)&timeout);
  • MARIADB_OPT_VERIFY_LOCAL_INFILE_CALLBACK: Specifies a callback function for filename and/or directory verification. This option was added in Connector/C 2.3.0

    int my_verify_filename(void *data, const char *filename)
    {
     return strcmp((char *)data, filename);
    }
    ...
    char *filename= "mydata.csv";
    mysql_optionsv(mysql, MARIADB_OPT_VERIFY_LOCAL_INFILE_CALLBACK, my_verify_filename, (void *)filename);
  • MYSQL_PROGRESS_CALLBACK: Specifies a callback function which will be able to visualize the progress of certain long running statements (i.e. LOAD DATA LOCAL INFILE or ALTER TABLE).

    static void report_progress(const MYSQL *mysql __attribute__((unused)),
     uint stage, uint max_stage,
     double progress __attribute__((unused)),
     const char *proc_info __attribute__((unused)),
     uint proc_info_length __attribute__((unused)))
    {
     ...
    }
    mysql_optionsv(mysql, MYSQL_PROGRESS_CALLBACK, (void *)report_progress);
  • MYSQL_OPT_RECONNECT: Enable or disable automatic reconnect.

    my_bool reconnect= 1; /* enable reconnect */
    mysql_optionsv(mysql, MYSQL_OPT_RECONNECT, (void *)&reconnect);
  • MYSQL_OPT_READ_TIMEOUT: Specifies the timeout in seconds for reading packets from the server.

    unsigned int timeout= 5;
    mysql_optionsv(mysql, MYSQL_OPT_READ_TIMEOUT, (void *)&timeout);
  • MYSQL_OPT_WRITE_TIMEOUT: Specifies the timeout in seconds for sending packets to the server.

    unsigned int timeout= 5;
    mysql_optionsv(mysql, MYSQL_OPT_WRITE_TIMEOUT, (void *)&timeout);
  • MYSQL_REPORT_DATA_TRUNCATION: Enable or disable reporting data truncation errors for prepared statements.

    mysql_optionsv(mysql, MYSQL_REPORT_DATA_TRUNCATION, NULL); /* disable */
    mysql_optionsv(mysql, MYSQL_REPORT_DATA_TRUNCATION, (void *)"1"); /* enable */
  • MYSQL_SET_CHARSET_DIR: character set files.

    mysql_optionsv(mysql, MYSQL_SET_CHARSET_DIR, (void *)"/usr/local/mysql/share/mysql/charsets");
  • MYSQL_SET_CHARSET_NAME: Specify the default character set for the connection.

    mysql_optionsv(mysql, MYSQL_SET_CHARSET_NAME, (void *)"utf8");
  • MYSQL_OPT_BIND: Specify the network interface from which to connect to MariaDB Server.

    mysql_optionsv(mysql, MYSQL_OPT_BIND, (void *)"192.168.8.3");
  • MYSQL_OPT_NONBLOCK: Specify stack size for non blocking operations. The argument for MYSQL_OPT_NONBLOCK is the size of the stack used to save the state of a non-blocking operation while it is waiting for I/O and the application is doing other processing. Normally, applications will not have to change this, and it can be passed as zero to use the default value.

    mysql_optionsv(mysql, MYSQL_OPT_NONBLOCK, 0);
  • MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS: If this option is set, the client indicates that it will be able to handle expired passwords by setting the CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS capability flag. If the password has expired and CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS is set, the server will not return an error when connecting, but put the connection in sandbox mode, where all commands will return error 1820 (ER_MUST_CHANGE_PASSWORD) unless a new password was set. This option was added in MariaDB Connector/C 3.0.4

    mysql_optionsv(mysql, MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, 1);
  • MYSQL_OPT_MAX_ALLOWED_PACKET: The maximum packet length to send to or receive from server. The default is 16MB, the maximum 1GB.

    mysql_optionsv(mysql, MYSQL_OPT_MAX_ALLOWED_PACKET, 0x40000000);
  • MYSQL_OPT_NET_BUFFER_LENGTH: The buffer size for TCP/IP and socket communication. Default is 16KB.

    mysql_optionsv(mysql, MYSQL_OPT_NET_BUFFER_LENGTH, 0x40000000);

Connection Options

Some of these options can also be set as arguments to the mysql_real_connect function.

  • MARIADB_OPT_HOST: Hostname or IP address of the server to connect to.

    mysql_optionsv(mysql, MARIADB_OPT_HOST, (void *)"dbserver.example.com");
  • MARIADB_OPT_USER: User to login to the server.

    mysql_optionsv(mysql, MARIADB_OPT_USER, (void *)"myuser");
  • MARIADB_OPT_PASSWORD: Password of the user to login to the server.

    mysql_optionsv(mysql, MARIADB_OPT_PASSWORD, (void *)"horsebattery");
  • MARIADB_OPT_SCHEMA: Database to use.

    mysql_optionsv(mysql, MARIADB_OPT_SCHEMA, (void *)"mydb");
  • MARIADB_OPT_PORT: Port number to use for connection.

    mysql_optionsv(mysql, MARIADB_OPT_PORT, 3307);
  • MARIADB_OPT_UNIXSOCKET: For connections to localhost, the Unix socket file to use, or, on Windows, the name of the named pipe to use.

    mysql_optionsv(mysql, MARIADB_OPT_UNIXSOCKET, (void *)"/var/lib/mysql/mysql.sock");
  • MYSQL_OPT_NAMED_PIPE: For Windows operating systems only: Use named pipes for client/server communication.

    mysql_optionsv(mysql, MYSQL_OPT_NAMED_PIPE, NULL);
  • MYSQL_OPT_PROTOCOL: Specify the type of client/server protocol. Possible values are:

    • MYSQL_PROTOCOL_TCP

    • MYSQL_PROTOCOL_SOCKET

    • MYSQL_PROTOCOL_PIPE

    • MYSQL_PROTOCOL_MEMORY.

      enum mysql_protocol_type prot_type= MYSQL_PROTOCOL_SOCKET;
      mysql_optionsv(mysql, MYSQL_OPT_PROTOCOL, (void *)&prot_type);
  • MARIADB_OPT_FOUND_ROWS: Return the number of matched rows instead of number of changed rows.

    mysql_optionsv(mysql, MARIADB_OPT_FOUND_ROWS, 1);
  • MYSQL_OPT_COMPRESS: Use the compressed protocol for client server communication. If the server doesn't support compressed protocol, the default protocol will be used.

    mysql_optionsv(mysql, MYSQL_OPT_COMPRESS, NULL);
  • MYSQL_OPT_LOCAL_INFILE: Enable or disable the use of LOAD DATA LOCAL INFILE

    unsigned int enable= 1, disable= 0;
    mysql_optionsv(mysql, MYSQL_OPT_LOCAL_INFILE, (void *)&disable);/* disable */
    mysql_optionsv(mysql, MYSQL_OPT_LOCAL_INFILE, (void *)NULL);     /* enable */
    mysql_optionsv(mysql, MYSQL_OPT_LOCAL_INFILE, (void *)&enable); /* enable */
  • MARIADB_OPT_MULTI_STATEMENTS: Allows the client to send multiple statements in one command. Statements will be divided by a semicolon.

    mysql_optionsv(mysql, MARIADB_OPT_MULTI_STATEMENTS, (void *)"");
  • MARIADB_OPT_MULTI_RESULTS: Indicates that the client is able to handle multiple result sets from stored procedures or multi statements. This option will be automatically set if MARIADB_OPT_MULTI_STATEMENTS is set.

    mysql_optionsv(mysql, MARIADB_OPT_MULTI_RESULTS, 1);
  • MYSQL_SHARED_MEMORY_BASE_NAME: Shared-memory name to use for Windows connections using shared memory to a local server (started with the --shared-memory option). Case-sensitive.

    mysql_optionsv(mysql, MYSQL_SHARED_MEMORY_BASE_NAME, (void *)"mariadb");

TLS Options

  • MYSQL_OPT_SSL_KEY: Defines a path to a private key file to use for TLS. This option requires that you use the absolute path, not a relative path. If the key is protected with a passphrase, the passphrase needs to be specified with MARIADB_OPT_TLS_PASSPHRASE option.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_KEY, (void *)"certs/client-key.pem");
  • MYSQL_OPT_SSL_CERT: Defines a path to the X509 certificate file to use for TLS. This option requires that you use the absolute path, not a relative path.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_CERT, (void *)"certs/client-cert.pem");
  • MYSQL_OPT_SSL_CA: Defines a path to a PEM file that should contain one or more X509 certificates for trusted Certificate Authorities (CAs) to use for TLS. This option requires that you use the absolute path, not a relative path. See Secure Connections Overview: Certificate Authorities (CAs) for more information.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_CA, (void *)"certs/ca-cert.pem");
  • MYSQL_OPT_SSL_CAPATH: Defines a path to a directory that contains one or more PEM files that should each contain one X509 certificate for a trusted Certificate Authority (CA) to use for TLS. This option requires that you use the absolute path, not a relative path. The directory specified by this option needs to be run through the openssl rehash command. See Secure Connections Overview: Certificate Authorities (CAs) for more information. This option is only supported if the connector was built with OpenSSL. If the connector was built with GnuTLS or Schannel, then this option is not supported. See TLS and Cryptography Libraries Used by MariaDB for more information about which libraries are used on which platforms.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_CAPATH, (void *)"certs/ca-cert.pem");
  • MYSQL_OPT_SSL_CIPHER: Defines a list of permitted ciphers or cipher suites to use for TLS.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_CIPHER, (void *)"DHE-RSA-AES256-SHA");
  • MYSQL_OPT_SSL_CRL: Defines a path to a PEM file that should contain one or more revoked X509 certificates to use for TLS. This option requires that you use the absolute path, not a relative path. See Secure Connections Overview: Certificate Revocation Lists (CRLs) for more information. This option is only supported if the connector was built with OpenSSL or Schannel. If the connector was built with GnuTLS, then this option is not supported. See TLS and Cryptography Libraries Used by MariaDB for more information about which libraries are used on which platforms.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_CAPATH, (void *)"certs/ca-cert.pem");\\\\<<code>>mysql_optionsv(mysql, MYSQL_OPT_SSL_CRL, (void *)"certs/crl.pem");
  • MYSQL_OPT_SSL_CRLPATH: Defines a path to a directory that contains one or more PEM files that should each contain one revoked X509 certificate to use for TLS. This option requires that you use the absolute path, not a relative path. The directory specified by this option needs to be run through the openssl rehash command. See Secure Connections Overview: Certificate Revocation Lists (CRLs) for more information. This option is only supported if the connector was built with OpenSSL. If the connector was built with GnuTLS or Schannel, then this option is not supported. See TLS and Cryptography Libraries Used by MariaDB for more information about which libraries are used on which platforms.

    mysql_optionsv(mysql, MYSQL_OPT_SSL_CAPATH, (void *)"certs/ca-cert.pem");\\\\<<code>>mysql_optionsv(mysql, MYSQL_OPT_SSL_CRLPATH, (void *)"certs/crls");
  • MARIADB_OPT_SSL_FP: Specify the SHA1 fingerprint of a server certificate for validation during the TLS handshake. This is deprecated. Use MARIADB_OPT_TLS_PEER_FP instead.

    mysql_optionsv(mysql, MARIADB_OPT_SSL_FP, (void *)"3a079e1a14ad326953a5d280f996b93d772a5bea");
  • MARIADB_OPT_TLS_PEER_FP: Specify the SHA1 fingerprint of a server certificate for validation during the TLS handshake.

    mysql_optionsv(mysql, MARIADB_OPT_TLS_PEER_FP, (void *)"3a079e1a14ad326953a5d280f996b93d772a5bea");
  • MARIADB_OPT_SSL_FP_LIST: Specify a file which contains one or more SHA1 fingerprints of server certificates for validation during the TLS handshake. This is deprecated. Use MARIADB_OPT_TLS_PEER_FP_LIST instead.

    mysql_optionsv(mysql, MARIADB_OPT_SSL_FP_LIST, (void *)"certs/fingerprints.txt");
  • MARIADB_OPT_TLS_PEER_FP_LIST: Specify a file which contains one or more SHA1 fingerprints of server certificates for validation during the TLS handshake.

    mysql_optionsv(mysql, MARIADB_OPT_TLS_PEER_FP_LIST, (void *)"certs/fingerprints.txt");
  • MARIADB_OPT_TLS_PASSPHRASE: Specify a passphrase for a passphrase-protected private key, as configured by the MYSQL_OPT_SSL_KEY option. This option is only supported if the connector was built with OpenSSL or GnuTLS. If the connector was built with Schannel, then this option is not supported. See TLS and Cryptography Libraries Used by MariaDB for more information about which libraries are used on which platforms.

    mysql_optionsv(mysql, MARIADB_OPT_SSL_PASSPHRASE, (void *)"thisisashortpassphrase");
  • MARIADB_OPT_TLS_VERSION: Defines which TLS protocol versions are allowed. This should be a comma-separated list of TLS protocol versions to allow. Valid TLS protocol versions are TLSv1.0, TLSv1.1, TLSv1.2, and TLSv1.3. Both the client and server should support the allowed TLS protocol versions. See Secure Connections Overview: TLS Protocol Version Support for information on which TLS libraries support which TLS protocol versions. See TLS and Cryptography Libraries Used by MariaDB for more information about which TLS libraries are used on which platforms.

    mysql_optionsv(mysql, MARIADB_OPT_TLS_VERSION, (void *)"TLSv1.2,TLSv1.3");
  • MYSQL_OPT_SSL_VERIFY_SERVER_CERT: Enables (or disables) server certificate verification.

    my_bool verify= 1;
    mysql_optionsv(mysql, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (void *)&verify);
  • MYSQL_OPT_SSL_ENFORCE: Whether to force TLS. This enables TLS with the default system settings. It does not prevent the connection from being created if the server does not support TLS.

    my_bool enforce_tls= 1;
    mysql_optionsv(mysql, MYSQL_OPT_SSL_ENFORCE, (void *)&enforce_tls);
  • MARIADB_OPT_TLS_CIPHER_STRENGTH: Cipher strength. This value will be passed as an unsigned int parameter.

    unsigned int cipher_strength= 128;
    mysql_optionsv(mysql, MARIADB_OPT_TLS_CIPHER_STRENGTH, (void*)&cipher_strength);

Plugin Options

  • MYSQL_DEFAULT_AUTH: Default authentication client-side plugin to use.

    mysql_optionsv(mysql, MYSQL_DEFAULT_AUTH, (void *)"ed25519");
  • MYSQL_ENABLE_CLEARTEXT_PLUGIN: This option is supported to be compatible with MySQL client libraries. MySQL client libraries use this option to determine whether the mysql_clear_password authentication plugin can be used. However, MariaDB clients and client libraries do not need to set any options in order to use this authentication plugin. Therefore, this option does not actually do anything in MariaDB Connector/C.

    mysql_optionsv(mysql, MYSQL_ENABLE_CLEARTEXT_PLUGIN, 1);
  • MARIADB_OPT_CONNECTION_HANDLER: Specify the name of a connection handler plugin.

    mysql_optionsv(mysql, MARIADB_OPT_CONNECTION_HANDLER, (void *)"aurora");
  • MARIADB_OPT_USERDATA: Bundle user data to the current connection, e.g. for use in connection handler plugins. This option requires 4 parameters: connection, option, key and value.

    mysql_optionsv(mysql, MARIADB_OPT_USERDATA, (void *)"ssh_user", (void *)ssh_user);
  • MARIADB_OPT_CONNECTION_READ_ONLY: This option is used by connection handler plugins and indicates that the current connection will be used for read operations only.

    my_bool read_only= 1;
    mysql_optionsv(mysql, MARIADB_OPT_CONNECTION_READ_ONLY, (void *)&read_only);
  • MYSQL_PLUGIN_DIR: Specify the location of client plugins. The plugin directory can also be specified with the MARIADB_PLUGIN_DIR environment variable.

    mysql_optionsv(mysql, MYSQL_PLUGIN_DIR, (void *)"/opt/mariadb/lib/plugins");
  • MYSQL_SECURE_AUTH: Refuse to connect to the server if the server uses the mysql_old_password authentication plugin. This mode is off by default, which is a difference in behavior compared to MySQL 5.6 and later, where it is on by default.

    mysql_optionsv(mysql, MYSQL_SECURE_AUTH, 1);
  • MYSQL_SERVER_PUBLIC_KEY: Specifies the name of the file which contains the RSA public key of the database server. The format of this file must be in PEM format. This option is used by the caching_sha2_password client authentication plugin. It was introduced in Connector/C 3.1.0.

    mysql_optionsv(mysql, MYSQL_SERVER_PUBLIC_KEY, (void *)(void *)"certs/server-cert.pem);

Option File Options

  • MYSQL_READ_DEFAULT_FILE: Read options from an option file.

  • MYSQL_READ_DEFAULT_GROUP: Read options from the named option group from an option file.

These options work together, according to the following rules:

  • if both are set to NULL, then no option files are read.

  • if MYSQL_READ_DEFAULT_FILE is set to an empty string (or NULL and MYSQL_READ_DEFAULT_GROUP is set) then all default option files are read.

  • if MYSQL_READ_DEFAULT_FILE is set to a non-empty string, then it is interpreted as a path to a custom option file, and only that option file is read.

  • if MYSQL_READ_DEFAULT_GROUP is an empty string (or NULL and MYSQL_READ_DEFAULT_FILE is set) then only default groups — [client], [client-server], [client-mariadb] are read.

  • if MYSQL_READ_DEFAULT_GROUP is a non-empty string, then it is interpreted as a custom option group, and that custom option group is read in addition to default groups from above.

Connection Attribute Options

Connection attributes are stored in the session_connect_attrs and session_account_connect_attrs Performance Schema tables. By default, MariaDB Connector/C sends the following connection attributes to the server:

  • _client_name: always "libmariadb"

  • _client_version: version of MariaDB Connector/C

  • _os: operation system

  • _pid: process id

  • _platform: e.g. x86 or x64

  • _server_host: the hostname (as specified in mysql_real_connect). This attribute was added in Connector/C 3.0.5

If the Performance Schema is disabled, connection attributes will not be stored on server.

  • MYSQL_OPT_CONNECT_ATTR_DELETE: Deletes a connection attribute for the given key.

    mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_DELETE, (void *)"app_version");
  • MYSQL_OPT_CONNECT_ATTR_ADD: Adds a key/value pair to connection attributes.

    mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_ADD, (void *)"app_version", (void *)"2.0.1");
  • MYSQL_OPT_CONNECT_ATTR_RESET: Clears the current list of connection attributes.

    mysql_optionsv(mysql, MYSQL_OPT_CONNECT_ATTR_RESET, 0);

See Also

  • mysql_init()

  • mysql_options()

  • mysql_real_connect()

mysql_ping

Syntax

int mysql_ping(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Checks whether the connection to the server is working. If it has gone down, and global option reconnect is enabled an automatic reconnection is attempted.

Returns zero on success, nonzero if an error occured.

This function can be used by clients that remain idle for a long while, to check whether the server has closed the connection and reconnect if necessary.

If a reconnect occurred the thread_id will change. Also resources bundled to the connection (prepared statements, locks, temporary tables, ...) will be released.

See also

  • mysql_options()

  • mysql_kill()

mysql_query

Syntax

int mysql_query(MYSQL * mysql,
                const char * query);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • query -a null terminated string containing the statement to be performed.

Description

Performs a statement pointed to by the null terminate string query against the database. Contrary to mysql_real_query(), mysql_query() is not binary safe.

Returns zero on success, non zero on failure

For executing multi statements the statements within the null terminated string statements must be separated by a semicolon.

If your statement contains binary data you should use mysql_real_query() or escape your data with mysql_hex_string().

To determine if a statement returned a result set use the function mysql_num_fields().

See also

  • mysql_real_query()

  • mysql_num_fields()

  • mysql_hex_string()

  • mysql_use_result()

  • mysql_store_result()

mysql_read_query_result

Syntax

int mysql_read_query_result(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Returns zero on success, otherwise non-zero.

Description

mysql_read_query_result() reads the result of a SQL statement executed with mysql_send_query(). If the SQL statement returned a resultset, it must be freed before the next call to mysql_read_query_result() is made. This is similar to how results from mysql_query() must be processed before another call can be made.

mysql_real_connect

Syntax

MYSQL * mysql_real_connect(MYSQL * mysql,
                           const char * host,
                           const char * user,
                           const char * passwd,
                           const char * db,
                           unsigned int port,
                           const char * unix_socket,
                           unsigned long flags);
  • mysql - a mysql handle, which was previously allocated by mysql_init().

  • host - can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.

  • user - the user name.

  • passwd - If provided or NULL, the server will attempt to authenticate the user against those user records which have no password only. This allows one username to be used with different permissions (depending on if a password as provided or not).

  • db - if provided will specify the default database to be used when performing queries.

  • port - specifies the port number to attempt to connect to the server.

  • unix_socket - specifies the socket or named pipe that should be used.

  • flags - the flags allows various connection options to be set:

    • CLIENT_FOUND_ROWS: Return the number of matched rows instead of number of changed rows.

    • CLIENT_NO_SCHEMA: Forbids the use of database.tablename.column syntax and forces the SQL parser to generate an error.

    • CLIENT_COMPRESS: Use compression protocol

    • CLIENT_IGNORE_SPACE: Allows spaces after function names. All function names will become reserved words.

    • CLIENT_LOCAL_FILES: Allows LOAD DATA LOCAL statements

    • CLIENT_MULTI_STATEMENTS: Allows the client to send multiple statements in one command. Statements will be divided by a semicolon.

    • CLIENT_MULTI_RESULTS: Indicates that the client is able to handle multiple result sets from stored procedures or multi statements. This option will be automatically set if CLIENT_MULTI_STATEMENTS is set.

    • And others per protocol capabilities.

Description

Establishes a connection to a database server. Returns a MYSQL * handle or NULL if an error occurred.

The password doesn't need to be encrypted before executing mysql_real_connect(). This will be handled in the client server protocol.

The connection handle can't be reused for establishing a new connection. It must be closed and reinitialized before.

See also

  • mariadb_reconnect

  • mysql_close()

  • mysql_init()

  • protocol capabilities

mysql_real_escape_string

Syntax

unsigned long mysql_real_escape_string(MYSQL * mysql,
                                       char * to,
                                       const char * from,
                                       unsigned long);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • to - buffer for the encoded string. The size of this buffer must be length * 2 + 1 bytes: in worst case every character of the from string needs to be escaped. Additionally a trailing 0 character will be appended.

  • from - a string which will be encoded by mysql_real_escape_string().

  • long - the length of the from string.

Description

This function is used to create a legal SQL string that you can use in an SQL statement. The given string is encoded to an escaped SQL string, taking into account the current character set of the connection.

Returns the length of the encoded (to) string.

mysql_real_query

Syntax

int mysql_real_query(MYSQL * mysql,
                     const char * q,
                     unsigned long);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • query - a string containing the statement to be performed.

  • long - length of the string.

Description

mysql_real_query() is the binary safe function for performing a statement on the database server. Returns zero on success, otherwise non-zero.

Contrary to the mysql_query() function, mysql_real_query is binary safe.

To determine if mysql_real_query returns a result set use the mysql_num_fields() function.

See also

  • mysql_query()

  • mysql_num_fields()

  • mysql_use_result()

  • mysql_store_result()

mysql_refresh

Syntax

int mysql_refresh(MYSQL * mysql,
  unsigned int options);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • options - a bit masked composed integer. See below.

Description

Flushes different types of information stored on the server. The bit-masked parameter options specify which kind of information will be flushed. options can be any combinationation of the following:

Option
Description

REFRESH_GRANT

Refresh grant tables.

REFRESH_LOG

Flush logs.

REFRESH_TABLES

Flush table cache.

REFRESH_HOSTS

Flush host cache.

REFRESH_STATUS

Reset status variables.

REFRESH_THREADS

Flush thread cache.

REFRESH_SLAVE

Reset master server information and restart slaves.

REFRESH_MASTER

Remove binary log files.

REFRESH_READ_LOCK

REFRESH_FAST

Returns zero on success, otherwise non zero.

To combine different values in the options parameter use the OR operator '|'. The function mysql_reload() is an alias for mysql_refresh().

mysql_reset_connection

Syntax

int mysql_reset_connection(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Resets the current connection and clears session state. Similar to mysql_change_user() or mariadb_reconnect(), mysql_reset_connection() resets session status, but without disconnecting, opening, or reauthenticating.

On client side mysql_reset_connection()

  • clears pending or unprocessed result sets

  • clears status like affected_rows, info or last_insert_id

  • invalidates active prepared statements

On server side mysql_reset_connection()

  • drops temporary table(s)

  • rollbacks active transaction

  • resets autocommit mode

  • releases table locks

  • initializes session variables (and sets them to the value of corresponding global variables)

  • closes active prepared statements

  • clears user variables

Returns zero on success, non zero if an error occurred.

This function was added in MariaDB Connector/C 3.0.0.

mysql_rollback

Syntax

my_bool mysql_rollback(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Rolls back the current transaction for the database. Returns zero on success, nonzero if an error occurred.

mysql_rollback() will not work as expected if autocommit mode was set or the storage engine does not support transactions.

See also

  • mysql_commit()

  • mysql_autocommit()

mysql_row_seek

Syntax

MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES * result,
    MYSQL_ROW_OFFSET offset);
  • result - a result set identifier returned by mysql_store_result().

  • offset - row offset. This value can be obtained either by mysql_row_seek() or mysql_row_tell()

Description

Positions the row cursor to an aribtrary row in a result set which was obtained by mysql_store_result(). Returns the previous row offset.

This function will not work if the result set was obtained by mysql_use_result().

See also

  • mysql_store_result()

  • mysql_row_tell()

mysql_row_tell

Syntax

MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES * res);
  • res - a result set identifier returned by mysql_store_result().

Description

Returns the row offset of a result cursor. The returned offset value can be used to reposition the result cursor by calling mysql_row_seek().

This function will not work if the result set was obtained by mysql_use_result().

See also

  • mysql_store_result()

  • mysql_row_seek()

mysql_select_db

Syntax

int mysql_select_db(MYSQL * mysql,
                    const char * db);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • db - the default database name

Description

Selects a database as default. Returns zero on success, non-zero on failure

The SQL command SELECT DATABASE() will return the name of the default database.

The default database can also be set by the db parameter in mysql_real_connect().

mysql_send_query

Syntax

int mysql_send_query(MYSQL * mysql,
                     const char * query,
                     unsigned long length);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • query - the query to execute.

  • length - length of query.

Returns zero on success, otherwise non-zero.

Description

mysql_send_query() executes a SQL statement without waiting for the result. The main purpose of this function is to perform batch execution of DML statements.

Each successful call tomysql_send_query() must be followed by a call to mysql_read_query_result(). Multiple calls to mysql_send_query() can be made before the calls to mysql_read_query_result() are done.

mysql_server_end

Syntax

void mysql_server_end(void );

Description

mysql_server_end() is an alias for mysql_library_end().

See also

  • mysql_library_init()

  • mysql_library_end()

mysql_server_init

Syntax

void mysql_server_init(void );

Description

mysql_server_init() is an alias for mysql_library_init().

See also

  • mysql_library_init()

  • mysql_library_end()

mysql_session_track_get_first

Syntax

int mysql_session_track_get_first(MYSQL * mysql,enum enum_session_state_type type, const char **data, size_t *length );
  • mysql - mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • type - type of information. Valid values are

    • SESSION_TRACK_SYSTEM_VARIABLES

    • SESSION_TRACK_SCHEMA

    • SESSION_TRACK_STATE_CHANGE

    • SESSION_TRACK_GTIDS (unsupported)

  • data - pointer to data, which must be declared as const char *

  • length - pointer to a size_t variable, which will contain the length of data

Description

mysql_session_track_get_first() retrieves the first session status change information received from the server.

Depending on the specified type the read only data pointer will contain the following information:

  • SESSION_TRACK_SCHEMA: The name of the default schema (database)

  • SESSION_TRACK_SYSTEM_VARIABLES: If a session system variable is changed, the first call contains the name of the changed system variable, the second call contains the new value. Both name and value are represented as strings.

  • SESSION_TRACK_STATE_CHANGE: shows whether the session status has changed. The value is changed as string "1" (changed) or "0" (unchanged).

Further data needs to be obtained by calling mysql_session_track_get_next().

mysql_session_track_get_first() was added in Connector/C 3.0 and MariaDB Server 10.2.

Returns

Zero for success, nonzero if an error occurred.

See also

mysql_session_track_get_next()

mysql_session_track_get_next

Syntax

int mysql_session_track_get_next(MYSQL * mysql,enum enum_session_state_type type, const char **data, size_t *length );
  • mysql - mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • type - type of information. Valid values are

    • SESSION_TRACK_SYSTEM_VARIABLES

    • SESSION_TRACK_SCHEMA

    • SESSION_TRACK_STATE_CHANGE

    • SESSION_TRACK_GTIDS (unsupported)

  • data - pointer to data, which must be declared as const char *

  • length - pointer to a size_t variable, which will contain the length of data

Description

mysql_session_track_get_next() retrieves the session status change information received from the server after a successful call to mysql_session_track_get_first().

mysql_session_track_get_next() needs to be called repeatedly until a non zero return value indicates end of data.

mysql_session_track_get_next() was added in Connector/C 3.0 and MariaDB Server 10.2.

Returns

Zero for success, nonzero if an error occurred.

See also

mysql_session_track_get_first()

mysql_set_character_set

Syntax

int mysql_set_character_set(MYSQL * mysql,
                            const char * csname);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • csname - character set name

Description

Sets the default character set for the current connection. Returns zero on success, non-zero on failure.

It's strongly recommended to use mysql_set_character_set() instead of SET NAMES ... since mysql_real_escape_string() might fail or deliver unexpected results.

The client library supports the following character sets:

Character set
Description

armscii8

8 bit character set for Armenian

ascii

US ASCII character set

big5

2 byte character set for traditional Chinese, Hongkong, Macau and Taiwan

binary

8 bit binary character set

cp1250

Windows code page 1250 character set

cp1251

Windows code page 1251 character set

cp1256

Windows code page 1256 character set

cp1257

Windows code page 1257 character set

cp850

MS-DOS Codepage 850 (Western Europe)

cp852

MS-DOS Codepage 852 (Middle Europe)

cp866

MS-DOS Codepage 866 (Russian)

cp932

Microsoft Codepage 932 (Extension to sjis)

dec8

DEC West European

eucjpms

UJIS for Windows Japanese

euckr

EUC KR-Korean

gb2312

GB-2312 simplified Chinese

gbk

GBK simplified Chinese

geostd8

GEOSTD8 Georgian

greek

ISO 8859-7 Greek

hebrew

ISO 8859-8 Hebrew

hp8

HP West European

keybcs2

DOS Kamenicky Czech-Slovak

koi8r

KOI8-R Relcom Russian

koi8u

KOI8-U Ukrainian

latin1

CP1252 Western European

latin2

ISO 8859-2 Central Europe

latin5

ISO 8859-9 Turkish

latin7

ISO 8859-13 Baltic

macce

MAC Central European

macroman

MAC Western European

sjis

SJIS for Windows Japanese

swe7

7-bit Swedish

tis620

TIS620 Thai

ucs2

UCS-2 Unicode

ujis

EUC-JP Japanese

utf8

UTF-8 Unicode

utf16

UTF-16 Unicode

utf32

UTF-32 Unicode

utf8mb4

UTF 4-byte Unicode

See also

  • mysql_real_escape_string()

mysql_set_server_option

Syntax

int mysql_set_server_option(MYSQL * mysql,
  enum enum_mysql_set_option);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • enum_mysql_set_option - server option (see below)

Description

Server option, which can be one of the following values:

Option
Description

MYSQL_OPTION_MULTI_STATEMENTS_OFF

Disables multi statement support

MYSQL_OPTION_MULTI_STATEMENTS_ON

Enable multi statement support

Returns zero on success, non-zero on failure.

See also

  • mysql_real_connect()

mysql_shutdown

Syntax

int mysql_shutdown(MYSQL * mysql,
  enum mysql_enum_shutdown_level);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • mysql_enum_shutdown_level - currently only one shutdown level, SHUTDOWN_DEFAULT is supported.

Description

Sends a shutdown message to the server. To shutdown the database server, the user for the current connection must have SHUTDOWN privileges.

Returns zero on success, non-zero on failure.

See also

  • mysql_kill()

mysql_sqlstate

Syntax

const char * mysql_sqlstate(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns a string containing the SQLSTATE error code for the most recently invoked function that can succeed or fail. The error code consists of five characters. '00000' means no error. The values are specified by ANSI SQL and ODBC

Please note that not all client library error codes are mapped to SQLSTATE errors. Errors which can't be mapped will returned as value HY000.

See also

  • mysql_error()

  • mysql_errno()

mysql_ssl_set

Syntax

my_bool mysql_ssl_set(MYSQL *mysql, const char *key, const char *cert,
  const char *ca, const char *capath, const char *cipher)
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

  • key - path to the key file.

  • cert - path to the certificate file.

  • ca - path to the certificate authority file.

  • capath - path to the directory containing the trusted TLS CA certificates in PEM format.

  • cipher list of permitted ciphers to use for TLS encryption.

Description

Used for establishing a secure TLS connection. It must be called before attempting to use mysql_real_connect(). TLS support must be enabled in the client library in order for the function to have any effect.

NULL can be used for an unused parameter. Always returns zero.

mysql_real_connect() will return an error if attempting to connect and TLS is incorrectly set up.

See also

  • mysql_get_ssl_cipher()

mysql_stat

Syntax

const char * mysql_stat(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

mysql_stat() returns a string with the current server status for uptime, threads, queries, open tables, flush tables and queries per second.

For a complete list of other status variables, you have to use the SHOW STATUS SQL command.

See also

  • mysql_get_server_info()

mysql_store_result

Syntax

MYSQL_RES * mysql_store_result(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns a buffered resultset from the last executed query.

mysql_store_result() returns NULL in case an error occured or if the query didn't return data (e.g. when executing an INSERT or UPDATE query.

mysql_field_count() indicates if there will be a result set available.

The memory allocated by mysql_store_result() needs to be released by calling the function mysql_free_result().

See also

  • mysql_use_result()

  • mysql_real_query()

  • mysql_field_count()

mysql_thread_end

Syntax

void mysql_thread_end(void );

Description

The mysql_thread_end() function needs to be called before a client thread ends. It will release thread specific memory, which was allocated by a previous mysql_thread_init() call. Returns void.

Unlike mysql_thread_init() mysql_thread_end() will not be invoked automatically if the thread ends. To avoid memory leaks mysql_thread_end() must be called explicitly.

See also

  • mysql_thread_init()

  • mysql_thread_safe()

mysql_thread_id

Syntax

unsigned long mysql_thread_id(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

The mysql_thread_id() function returns the thread id for the current connection.

The current connection can be killed with mysql_kill(). If reconnect option is enabled the thread id might change if the client reconnects to the server.

Note that connector will return only the first 32bits value. If your database might expect to create more than 4.3 billion connection without a restart, it's better to query 'select CONNECTION_ID()'

See also

  • mysql_kill()

  • mysql_options()

mysql_thread_init

Syntax

my_bool mysql_thread_init(void );

Description

Thread initialization for multi threaded clients. Multi threaded clients should call mysql_thread_init() at the beginning of the thread initialization to initialize thread specific client library variables. If mysql_thread_init() was not called explicitly, it will be called automatically by mysql_init() or mysql_real_connect().

Returns zero if successful or 1 if an error occurred.

Before a client thread ends the mysql_thread_end() function must be called to release memory - otherwise the client library will report an error.

See also

  • mysql_thread_end()

  • mysql_thread_safe()

mysql_thread_safe

Syntax

unsigned int mysql_thread_safe(void );

Description

Indicates whether or not the client library is compiled as thread safe. Returns 1 if the client library was compiled as thread safe otherwise zero.

By default the mariadb client library is compiled as thread safe.

See also

  • mysql_thread_init()

  • mysql_thread_end()

mysql_use_result

Syntax

MYSQL_RES * mysql_use_result(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Used to initiate the retrieval of a result set from the last query executed using the mysql_real_query() function on the database connection. Either this or the mysql_store_result() function must be called before the results of a query can be retrieved, and one or the other must be called to prevent the next query on that database connection from failing.

Returns an unbuffered result set or NULL if an error occurred.

The mysql_use_result() function does not transfer the entire result set. Hence several functions like mysql_num_rows() or mysql_data_seek() cannot be used.

mysql_use_result() will block the current connection until all result sets are retrieved or result set was released by mysql_free_result().

See also

  • mysql_store_result()

  • mysql_free_result()

mysql_warning_count

Syntax

unsigned int mysql_warning_count(MYSQL * mysql);
  • mysql - a mysql handle, which was previously allocated by mysql_init() or mysql_real_connect().

Description

Returns the number of warnings from the last executed query, or zero if there are no warnings.

For retrieving warning messages you should use the SQL command SHOW WARNINGS. If SQL_MODE TRADITIONAL is enabled an error instead of warning will be returned. For detailed information check the server documentation.

See also

  • mysql_stmt_affected_rows()