OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Connector/C

V2.0.0

  • Overview
  • Installation guide
  • User Guide
    • Basic data structures
    • Basic operations
      • Basic API operations
      • API operations on prepared statements
  • C API functions
    • Overview
    • my_init()
    • mysql_affected_rows()
    • mysql_autocommit()
    • mysql_change_user()
    • mysql_character_set_name()
    • mysql_client_find_plugin()
    • mysql_client_register_plugin()
    • mysql_close()
    • mysql_commit()
    • mysql_connect()
    • mysql_create_db()
    • mysql_data_seek()
    • mysql_debug()
    • mysql_drop_db()
    • mysql_dump_debug_info()
    • mysql_eof()
    • mysql_errno()
    • mysql_error()
    • mysql_escape_string()
    • mysql_fetch_field()
    • mysql_fetch_field_direct()
    • mysql_fetch_fields()
    • mysql_fetch_lengths()
    • mysql_fetch_row()
    • mysql_field_count()
    • mysql_field_seek()
    • mysql_field_tell()
    • mysql_free_result()
    • mysql_get_character_set_info()
    • mysql_get_client_info()
    • mysql_get_client_version()
    • mysql_get_host_info()
    • mysql_get_proto_info()
    • mysql_get_server_info()
    • mysql_get_server_version()
    • mysql_get_ssl_cipher()
    • mysql_hex_string()
    • mysql_info()
    • mysql_init()
    • mysql_insert_id()
    • mysql_kill()
    • mysql_library_end()
    • mysql_library_init()
    • mysql_list_dbs()
    • mysql_list_fields()
    • mysql_list_processes()
    • mysql_list_tables()
    • mysql_load_plugin()
    • mysql_load_plugin_v()
    • mysql_more_results()
    • mysql_next_result()
    • mysql_num_fields()
    • mysql_num_rows()
    • mysql_options()
    • mysql_options4()
    • mysql_ping()
    • mysql_plugin_options()
    • mysql_query()
    • mysql_real_connect()
    • mysql_real_escape_string()
    • mysql_real_query()
    • mysql_refresh()
    • mysql_reload()
    • mysql_rollback()
    • mysql_row_seek()
    • mysql_row_tell()
    • mysql_select_db()
    • mysql_server_end()
    • mysql_server_init()
    • mysql_set_character_set()
    • mysql_set_local_infile_default()
    • mysql_set_local_infile_handler()
    • mysql_set_server_option()
    • mysql_shutdown()
    • mysql_sqlstate()
    • mysql_ssl_set()
    • mysql_stat()
    • mysql_stmt_affected_rows()
    • mysql_stmt_attr_get()
    • mysql_stmt_attr_set()
    • mysql_stmt_bind_param()
    • mysql_stmt_bind_result()
    • mysql_stmt_close()
    • mysql_stmt_data_seek()
    • mysql_stmt_errno()
    • mysql_stmt_error()
    • mysql_stmt_execute()
    • mysql_stmt_fetch()
    • mysql_stmt_fetch_column()
    • mysql_stmt_field_count()
    • mysql_stmt_free_result()
    • mysql_stmt_init()
    • mysql_stmt_insert_id()
    • mysql_stmt_next_result()
    • mysql_stmt_num_rows()
    • mysql_stmt_param_count()
    • mysql_stmt_param_metadata()
    • mysql_stmt_prepare()
    • mysql_stmt_reset()
    • mysql_stmt_result_metadata()
    • mysql_stmt_row_seek()
    • mysql_stmt_row_tell()
    • mysql_stmt_send_long_data()
    • mysql_stmt_sqlstate()
    • mysql_stmt_store_result()
    • mysql_store_result()
    • mysql_thread_end()
    • mysql_thread_id()
    • mysql_thread_init()
    • mysql_thread_safe()
    • mysql_use_result()
    • mysql_warning_count()

Download PDF

Overview Installation guide Basic data structures Basic API operations API operations on prepared statements Overview my_init() mysql_affected_rows() mysql_autocommit() mysql_change_user() mysql_character_set_name() mysql_client_find_plugin() mysql_client_register_plugin() mysql_close() mysql_commit() mysql_connect() mysql_create_db() mysql_data_seek() mysql_debug() mysql_drop_db() mysql_dump_debug_info() mysql_eof() mysql_errno() mysql_error() mysql_escape_string() mysql_fetch_field() mysql_fetch_field_direct() mysql_fetch_fields() mysql_fetch_lengths() mysql_fetch_row() mysql_field_count() mysql_field_seek() mysql_field_tell() mysql_free_result() mysql_get_character_set_info() mysql_get_client_info() mysql_get_client_version() mysql_get_host_info() mysql_get_proto_info() mysql_get_server_info() mysql_get_server_version() mysql_get_ssl_cipher() mysql_hex_string() mysql_info() mysql_init() mysql_insert_id() mysql_kill() mysql_library_end() mysql_library_init() mysql_list_dbs() mysql_list_fields() mysql_list_processes() mysql_list_tables() mysql_load_plugin() mysql_load_plugin_v() mysql_more_results() mysql_next_result() mysql_num_fields() mysql_num_rows() mysql_options() mysql_options4() mysql_ping() mysql_plugin_options() mysql_query() mysql_real_connect() mysql_real_escape_string() mysql_real_query() mysql_refresh() mysql_reload() mysql_rollback() mysql_row_seek() mysql_row_tell() mysql_select_db() mysql_server_end() mysql_server_init() mysql_set_character_set() mysql_set_local_infile_default() mysql_set_local_infile_handler() mysql_set_server_option() mysql_shutdown() mysql_sqlstate() mysql_ssl_set() mysql_stat() mysql_stmt_affected_rows() mysql_stmt_attr_get() mysql_stmt_attr_set() mysql_stmt_bind_param() mysql_stmt_bind_result() mysql_stmt_close() mysql_stmt_data_seek() mysql_stmt_errno() mysql_stmt_error() mysql_stmt_execute() mysql_stmt_fetch() mysql_stmt_fetch_column() mysql_stmt_field_count() mysql_stmt_free_result() mysql_stmt_init() mysql_stmt_insert_id() mysql_stmt_next_result()
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. OceanBase Connector/C
  3. V2.0.0
iconOceanBase Connector/C
V 2.0.0
  • V 2.0.0

API operations on prepared statements

Last Updated:2026-04-10 04:01:40  Updated
share
What is on this page
share

You can use a prepared statement to efficiently execute it multiple times. The statement is parsed and then executed once or multiple times by using the statement handle returned by the initialization function.

An application uses OceanBase Connector/C to prepare and execute a statement by performing the following steps:

  1. Use mysql_stmt_init() to create a prepared statement handle. Call mysql_stmt_prepare() to pass a string that contains the SQL statement to the OBServer.

  2. Use mysql_stmt_bind_param() to bind values to parameters. All parameters must be bound to values. Otherwise, the statement execution returns an error or generates unexpected results.

  3. Call mysql_stmt_execute() to execute the statement.

  4. If the statement is SELECT or any other statement that generates a result set, call mysql_stmt_result_metadata() if necessary to obtain the result set metadata.

  5. If the statement generates a result set, call mysql_stmt_bind_result() to bind the data buffer for retrieving row values.

  6. Repeatedly call mysql_stmt_fetch() to pull data row by row into the buffer until the data of all rows are pulled.

  7. Repeat steps 3 to 6 as needed. You can repeatedly call mysql_stmt_execute() to execute the statement again by changing the parameter values in the corresponding buffers provided by mysql_stmt_bind_param().

  8. After the statement is executed, use mysql_stmt_close() to close the statement handle and release all associated resources. After that, the handle becomes invalid and cannot be used again.

  9. If you obtain the result set metadata of the SELECT statement by calling mysql_stmt_result_metadata(), you need to use mysql_free_result() to release the metadata.

For more information about APIs, see C API functions.

Example:

MYSQL_STMT *stmt;
MYSQL_BIND ps_params[3];    /* Buffer of input parameters. */
int        int_data[3];         /* Input/output values. */
my_bool    is_null[3];      /* The output value can be nullable. */
int        status;

/* Set the stored procedure. */
status = mysql_query(mysql, "DROP PROCEDURE IF EXISTS proc1");
test_error(mysql, status);

status = mysql_query(mysql,
  "CREATE PROCEDURE proc1("
  "  IN p_in INT, "
  "  OUT p_out INT, "
  "  INOUT p_inout INT) "
  "BEGIN "
  "  SELECT p_in, p_out, p_inout; "
  "  SET p_in = 110, p_out = 220, p_inout = 330; "
  "  SELECT p_in, p_out, p_inout; "
  "END");
test_error(mysql, status);

/* Use parameter placeholders to initialize and prepare the CALL statement. */
stmt = mysql_stmt_init(mysql);
if (!stmt)
{
  printf("Could not initialize statement\n");
  exit(1);
}
status = mysql_stmt_prepare(stmt, "CALL proc1(?, ?, ?)", 12);
test_stmt_error(stmt, status);

/* Initialize the parameters: p_in, p_out, and p_inout (all INT). */
memset(ps_params, 0, sizeof (ps_params));

ps_params[0].buffer_type = MYSQL_TYPE_LONG;
ps_params[0].buffer = (char *) &int_data[0];
ps_params[0].length = 0;
ps_params[0].is_null = 0;

ps_params[1].buffer_type = MYSQL_TYPE_LONG;
ps_params[1].buffer = (char *) &int_data[1];
ps_params[1].length = 0;
ps_params[1].is_null = 0;

ps_params[2].buffer_type = MYSQL_TYPE_LONG;
ps_params[2].buffer = (char *) &int_data[2];
ps_params[2].length = 0;
ps_params[2].is_null = 0;

/*Bind the parameters.*/
status = mysql_stmt_bind_param(stmt, ps_params);
test_stmt_error(stmt, status);

/* Assign values to parameters and execute the statement. */
int_data[0]= 10;     /* p_in */
int_data[1]= 20;     /* p_out */
int_data[2]= 30;     /* p_inout */

status = mysql_stmt_execute(stmt);
test_stmt_error(stmt, status);

/* Process the result until no rows can be found. */
do {
  int i;
  int num_counts;               /* The number of columns in the result. */
  MYSQL_FIELD *fields;     /* This is used for the result set metadata. */
  MYSQL_BIND *rs_bind;   /* This is used for the output buffer. */

  /* If a result set is generated, the number of columns is greater than 0. */
  /* If no result set is generated, the number of columns is 0. */
  num_fields = mysql_stmt_field_count(stmt);

  if (num_counts> 0)
  {
    /* One result set needs to be fetched. */
    printf("Number of columns in result: %d\n", (int) num_counts);

    /* Determine the type of the result set. */
    printf("Data:");
    if(mysql->server_status & SERVER_PS_OUT_PARAMS)
      printf("The result set contains OUT/INOUT parameters\n");
    else
      printf("The result set is generated by procedure\n");

    MYSQL_RES *rs_metadata = mysql_stmt_result_metadata(stmt);
    test_stmt_error(stmt, rs_metadata == NULL);

    fields = mysql_fetch_fields(rs_metadata);

    rs_bind = (MYSQL_BIND *) malloc(sizeof (MYSQL_BIND) * num_counts);
    if (!rs_bind)
    {
      printf("Cannot allocate output buffers\n");
      exit(1);
    }
    memset(rs_bind, 0, sizeof (MYSQL_BIND) * num_counts);

    /* Set and bind the result set output buffer. */
    for (i = 0; i < num_counts; ++i)
    {
      rs_bind[i].buffer_type = fields[i].type;
      rs_bind[i].is_null = &is_null[i];

      switch (fields[i].type)
      {
        case MYSQL_TYPE_LONG:
          rs_bind[i].buffer = (char *) &(int_data[i]);
          rs_bind[i].buffer_length = sizeof (int_data);
          break;

        default:
          fprintf(stderr, "ERROR: unexpected type: %d.\n", fields[i].type);
          exit(1);
      }
    }

    status = mysql_stmt_bind_result(stmt, rs_bind);
    test_stmt_error(stmt, status);

    /* Obtain and display rows of the result set. */
    while (1)
    {
      status = mysql_stmt_fetch(stmt);

      if (status == 1  status == MYSQL_NO_DATA)
        break;

      for (i = 0; i < num_fields; ++i)
      {
        switch (rs_bind[i].buffer_type)
        {
          case MYSQL_TYPE_LONG:
            if (*rs_bind[i].is_null)
              printf(" val[%d] = NULL;", i);
            else
              printf(" val[%d] = %ld;",
                     i, (long) *((int *) rs_bind[i].buffer));
            break;

          default:
            printf("  unexpected type (%d)\n",
              rs_bind[i].buffer_type);
        }
      }
      printf("\n");
    }

    mysql_free_result(rs_metadata);    /* Release the metadata. */
    free(rs_bind);                           /* Release the output buffer. */
  }
  else
  {
    /* No column = final status package. */
    printf("End of procedure output\n");
  }

  /* 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 > 0)
    test_stmt_error(stmt, status);
} while (status == 0);

mysql_stmt_close(stmt);

Previous topic

Basic API operations
Last

Next topic

Overview
Next