This topic describes how to build an application by using OceanBase Connector/ODBC and OceanBase Cloud to perform basic operations such as creating tables, inserting data, and querying data.
Download the c-oceanbase-odbc sample project Prerequisites
You have registered an OceanBase Cloud account, created an instance and an Oracle-compatible tenant. For more information, see Create an instance and Create a tenant.
You have installed Visual Studio.
You have installed the OceanBase Connector/ODBC driver.
Note
Download the OceanBase ODBC driver for Windows from the Middleware section on the Download Center of the OceanBase Cloud website. The OceanBase Connector/ODBC driver for Windows is a one-click installation package. You can install it by following the default instructions.
Procedure
Note
The following steps are for compiling and running the project in the Windows environment by using Visual Studio Community 2019. If you use other operating systems or compilers, the steps may vary.
- Open the
c-oceanbase-odbcproject. - Configure the project properties.
- Obtain the connection information of the OceanBase Cloud database.
- Modify the database connection information in the
c-oceanbase-odbcproject. - Build the project.
- Run the application.
- View the output.
Step 1: Open the c-oceanbase-odbc project
Start Visual Studio Community 2019.
Open an existing project.
On the start page of Visual Studio Community 2019, click Open Project or Solution (P) below Get Started. Alternatively, click Continue Without Code (W) below Get Started and select File > Open > Project/Solution (P) from the menu bar.
Navigate to the folder of the c-oceanbase-odbc project, select the project file (
c-oceanbase-odbc.slnorc-oceanbase-odbc.vcxproj), and click Open.
Step 2: Configure the project properties
Open the project properties.
Right-click the selected project in the Solution Explorer and select Attribute from the shortcut menu. Alternatively, select Projects > Attribute from the menu bar, or use the shortcut key Alt + Enter.
Configure the configuration manager.
In the properties page, select the Configuration (C) drop-down menu at the top and choose Debug.
In the properties page, select the Platform (P) drop-down menu at the top and choose x64.
Set the character set.
In the properties page, select the Advanced tab and find the Character Set section. In the drop-down menu, select Use Multi-Byte Character Set.
Step 3: Obtain the connection information of the OceanBase Cloud database
Log in to the OceanBase Cloud console. In the instance list, expand the information of the target instance, go to the target tenant, and choose Connect > Get Connection String.
For more information, see Get a connection string.
Fill in the corresponding information in the URL based on the created OceanBase Cloud database.
Here is an example:
obclient -h t********.********.oceanbase.cloud -P 1521 -u oracle001 -p******Parameter description:
-h: specifies the connection address of the OceanBase Cloud database, for example,t********.********.oceanbase.cloud.-P: specifies the connection port of the OceanBase Cloud database. The default value is 1521.-u: specifies the account for accessing the database.-p: specifies the password of the account.
For more information about the connection string, see Connect to an OceanBase tenant by using OBClient.
Step 4: Modify the database connection information in the c-oceanbase-odbc project
Modify the database connection information in the test_tbl1.cpp file based on the information obtained in Step 3: Obtain the connection information of the OceanBase Cloud database.
Here is an example:
char* mydriver = (char*)"Driver={OceanBase ODBC 2.0 Driver};Server=t********.********.oceanbase.cloud;Port=1521;Database=sys;User=oracle001;Password=******;Option=3;";
Step 5: Build the project
Select Build and then Build Solution. The compiler output and any error or warning information will be displayed during the build process.
Step 6: Run the application
Select Debug and then Start Debugging or Start Without Debugging to run the application.
Step 7: View the output
The output will be displayed in the debug console. You can process the output based on the program's design logic and code.
Project code
Click c-oceanbase-odbc to download the project code, which is a compressed file named c-oceanbase-odbc.zip.
After decompressing the file, you will find a folder named c-oceanbase-odbc. The directory structure is as follows:
c-oceanbase-odbc
├─ c-oceanbase-odbc.sln
├─ c-oceanbase-odbc.vcxproj
├─ c-oceanbase-odbc.vcxproj.filters
├─ c-oceanbase-odbc.vcxproj.user
└─ test_tbl1.cpp
File description:
c-oceanbase-odbc.sln: a solution file for Visual Studio that manages one or more projects.c-oceanbase-odbc.vcxproj: a project file for Visual Studio that describes the structure and configuration of a C/C++ project.c-oceanbase-odbc.vcxproj.filters: a project filter file for Visual Studio that defines the directory structure and organization of files in the project.c-oceanbase-odbc.vcxproj.user: a file for storing user-specific project settings.test_tbl1.cpp: a source code file that defines the structure of a data table and implements data table operations.
Introduction to the test_tbl1.cpp file
The test_tbl1.cpp file is used to define a data table named test_tbl1. It implements the creation, insertion, and query operations of the data table.
The code in the test_tbl1.cpp file mainly includes the following parts:
Include header files.
Include the header files
stdio.h,assert.h,windows.h,sql.h, andsqlext.h.Sample code:
#include <stdio.h> #include <assert.h> #include <windows.h> #include <sql.h> #include <sqlext.h>Define the
odbc_print_errorfunction.This function is used to print error information when an ODBC-related error occurs during program execution. The specific steps are as follows:
- Define the function name as
odbc_print_errorand specify the parametersSQLSMALLINT HandleTypeandSQLHANDLE Handle, which are used to specify the handle type and handle corresponding to the error information. - Define variables to store the error information obtained from ODBC. The types and names of these variables are defined in the ODBC API.
- Use the
SQLGetDiagRec()function to obtain the last error information generated by ODBC. After this function call, theSQLState,NativeError, andSQLMessagearrays store the relevant content of the error information. - Use the
fprintffunction to print the error information to the standard output stream.[%s] (%d) %s\nis the print format, which uses a formatted string similar to theprintffunction.%sis used to print string-type data,%dis used to print integer data, and\nindicates a line break.SQLState,NativeError, andSQLMessagecorrespond to the three parameters in the formatted string, representing the content of the error information to be printed.
Sample code:
static void odbc_print_error(SQLSMALLINT HandleType, SQLHANDLE Handle) { SQLCHAR SQLState[6]; SQLINTEGER NativeError; SQLCHAR SQLMessage[SQL_MAX_MESSAGE_LENGTH] = { 0 }; SQLSMALLINT TextLengthPtr; SQLGetDiagRec(HandleType, Handle, 1, SQLState, &NativeError, SQLMessage, SQL_MAX_MESSAGE_LENGTH, &TextLengthPtr); fprintf(stdout, "[%s] (%d) %s\n", SQLState, NativeError, SQLMessage); }- Define the function name as
Define the
ASSERT_CHECKfunction.This function is used to check and handle the return values of ODBC function calls. It checks whether an error occurs in the return value of the ODBC API function. If an error occurs, it prints the error information and exits the program. The specific steps are as follows:
- Define the macro name as
ASSERT_CHECKand specify the parametersSQLSMALLINT HandleType,SQLHANDLE Handle, andSQLRETURN rcode, which are the ODBC handle type, ODBC handle, and return value of the ODBC API function call, respectively. - Use the
ifstatement to judge the return value of the ODBC API function. If the return value is not equal toSQL_SUCCESSorSQL_SUCCESS_WITH_INFO, it indicates that an error occurred in the function call. - Call the
odbc_print_errorfunction to print the ODBC error information. - Call the
assertfunction to perform an assertion check during program execution. If an error occurs in the ODBC function call, the program will be interrupted.
Sample code:
static void ASSERT_CHECK(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLRETURN rcode) { if (rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO) { odbc_print_error(HandleType, Handle); assert(0); } }- Define the macro name as
Define the
mainfunction.Define the entry function
mainof the program, which returns an integer value. In themainfunction, write code related to database connection and data operations.Sample code:
int main() { // Apply for an environment handle // Set the ODBC version environment attribute // Allocate a connection handle // Connect to the data source // Create a table // Insert data // Query data // Clean up, release specific resource handles }Define variables.
Define the variable
henv, which is an object of the ODBC environment handle type, used to manage ODBC connections and resource allocation.OutConnStrandOutConnStrLenare used to store the connection string and string length, respectively. These parameters are passed to theSQLDriverConnectfunction when connecting to the database to obtain the return value and other connection information upon successful database connection.Sample code:
HENV henv; SQLCHAR OutConnStr[255]; SQLSMALLINT OutConnStrLen;Apply for an environment handle.
Use the
SQLAllocHandlefunction to allocate an ODBC environment handle and use theASSERT_CHECKfunction to check the allocation result to ensure the allocation is successful. The specific steps are as follows:After the
SQLAllocHandlefunction is executed, it returns aSQLRETURNtype value indicating the execution result of the function. In the code, this return value is stored in thercodevariable. The parameters of theSQLAllocHandlefunction are as follows:SQL_HANDLE_ENV: specifies the type of the handle to be allocated. Here, it isSQL_HANDLE_ENV, indicating that an environment handle is to be allocated.SQL_NULL_HANDLE: specifies the parent handle. Here, it isSQL_NULL_HANDLE, indicating that there is no parent handle.&henv: is a pointer to the variable that stores the allocated handle. Here, it ishenv, the variable for the environment handle.
The
ASSERT_CHECKfunction is used to check the return valuercodeof theSQLAllocHandlefunction. Ifrcodeis notSQL_SUCCESSorSQL_SUCCESS_WITH_INFO, indicating that the handle allocation failed, theASSERT_CHECKfunction will call theodbc_print_errorfunction to print the error information and use theassertmacro to terminate the program execution.
Sample code:
SQLRETURN rcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv); ASSERT_CHECK(SQL_HANDLE_ENV, henv, rcode);Set the ODBC version environment attribute.
Use the
SQLSetEnvAttrfunction to set the ODBC environment attribute, which can set some environment-level attributes to affect the behavior of database connections and drivers, and use theASSERT_CHECKfunction to check the setting result to ensure the setting is successful. The specific steps are as follows:After the
SQLSetEnvAttrfunction is executed, it returns aSQLRETURNtype value indicating the execution result of the function. In the code, this return value is stored in thercodevariable. The parameters of theSQLSetEnvAttrfunction are as follows:henv: is the ODBC environment handle, specifying the environment object to set the attribute.SQL_ATTR_ODBC_VERSION: specifies the type of the environment attribute to be set. Here, it isSQL_ATTR_ODBC_VERSION, indicating that the ODBC version attribute is to be set.(void*)SQL_OV_ODBC3: is a pointer to the attribute value. Here, it isSQL_OV_ODBC3, indicating that the ODBC version is to be set to the ODBC 3.x version.0: is the length of the attribute value. Here, since the attribute value is an enumeration constant, the length is0.
The
ASSERT_CHECKfunction is used to check the return valuercodeof theSQLSetEnvAttrfunction. Ifrcodeis notSQL_SUCCESSorSQL_SUCCESS_WITH_INFO, indicating that the attribute setting failed, theASSERT_CHECKfunction will call theodbc_print_errorfunction to print the error information and use theassertmacro to terminate the program execution.
Sample code:
rcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); ASSERT_CHECK(SQL_HANDLE_ENV, henv, rcode);Allocate a connection handle.
Use the
SQLAllocHandlefunction to allocate anODBCconnection handle and use theASSERT_CHECKfunction to check the allocation result to ensure the allocation is successful. The specific steps are as follows:After the
SQLAllocHandlefunction is executed, it returns a value of theSQLRETURNtype, indicating the execution result of the function. In the code, this return value is stored in thercodevariable. The parameters of theSQLAllocHandlefunction are as follows:SQL_HANDLE_DBC: specifies the type of the handle to be allocated. In this case, it isSQL_HANDLE_DBC, indicating that a connection handle is to be allocated.henv: is an allocated ODBC environment handle, which serves as the parent handle for associating with the connection handle.&hdbc: is a pointer to the variable that stores the allocated connection handle.
The
ASSERT_CHECKfunction is used to check the return valuercodeof theSQLAllocHandlefunction. Ifrcodeis notSQL_SUCCESSorSQL_SUCCESS_WITH_INFO, indicating that the handle allocation failed, theASSERT_CHECKfunction will call theodbc_print_errorfunction to print the error message and use theassertmacro to terminate the program.
Sample code:
SQLHDBC hdbc; rcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc); ASSERT_CHECK(SQL_HANDLE_DBC, hdbc, rcode);Connect to a data source.
Use the
SQLDriverConnectfunction to connect to the specified ODBC data source, and use theASSERT_CHECKfunction to check the result of the connection operation to ensure that the connection is successful. The specific steps are as follows:Define a connection string
mydriverto describe the detailed information required to connect to the database. The string contains the following parts:Driver={OceanBase ODBC 2.0 Driver}: specifies the ODBC driver to be used as OceanBase ODBC 2.0 Driver.Server=your_ip: specifies the connection address of the database.Port=your_port: specifies the port number used to connect to the database.Database=your_schema: specifies the name of the database to be connected.User=your_use: specifies the username used to connect to the database.Password=your_password: specifies the password used to connect to the database.Option=3: specifies the connection options, with a value of3, indicating the use of TCP/IP for connection.
Use the
SQLDriverConnectfunction to connect to the specified ODBC data source. After the function is executed, it returns a value of the SQLRETURN type, indicating the execution result of the function. In the code, this return value is stored in thercodevariable. The parameters of theSQLDriverConnectfunction are as follows:hdbc: is the ODBC connection handle, used to establish a connection with the data source.NULL: is a reserved parameter and is not used.mydriver: is the connection string, containing all the information required to connect to the database, such as the driver name, database address, port number, database name, username, and password.strlen((char*)mydriver) + 1: is the length of the connection string.OutConnStr: is a buffer of the SQLCHAR type used to store the connection string.255: is the length of the buffer.&OutConnStrLen: is a pointer to the variable that stores the actual length of the connection string.SQL_DRIVER_NOPROMPT: is a connection flag used to indicate that no prompt boxes are displayed during the connection.
The
ASSERT_CHECKfunction is used to check the return valuercodeof theSQLDriverConnectfunction. Ifrcodeis notSQL_SUCCESSorSQL_SUCCESS_WITH_INFO, indicating that the connection failed, theASSERT_CHECKfunction will call theodbc_print_errorfunction to print the error message and use theassertmacro to terminate the program.
Sample code:
char* mydriver = (char*)"Driver={OceanBase ODBC 2.0 Driver};Server=your_ip;Port=your_port;Database=your_schema;User=your_use;Password=your_password;Option=3;"; rcode = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)mydriver, strlen((char*)mydriver) + 1, OutConnStr, 255, &OutConnStrLen, SQL_DRIVER_NOPROMPT); ASSERT_CHECK(SQL_HANDLE_DBC, hdbc, rcode);Parameters required for connecting to an OceanBase Cloud database:
your_ip: the connection address of the OceanBase Cloud database, obtained from the-hparameter in the connection string.your_port: the connection port of the OceanBase Cloud database, obtained from the-Pparameter in the connection string.your_schema: the name of the schema to be accessed.your_user: the account name, obtained from the-uparameter in the connection string.your_password: the account password, obtained from the-pparameter in the connection string.
Create a table.
Use the
SQLAllocHandlefunction to allocate an ODBC statement handle, and use theSQLExecDirectfunction to execute an SQL statement to create a table. The code also uses theASSERT_CHECKfunction to check the results of the handle allocation and statement execution to ensure the operations are successful. Finally, use theSQLFreeHandlefunction to release the statement handle and free the resources. The specific steps are as follows:Declare an ODBC statement handle
stmt.Use the
SQLAllocHandlefunction to allocate an ODBC statement handle.Use the
ASSERT_CHECKfunction to check the return valuercodeof theSQLAllocHandlefunction to ensure that the handle allocation is successful.Use the
SQLExecDirectfunction to execute an SQL statement. In this case, it executes aCREATE TABLEstatement to create a table namedtest_tbl1.Use the
ASSERT_CHECKfunction to check the return valuercodeof theSQLExecDirectfunction to ensure that the statement execution is successful.Use the
SQLFreeHandlefunction to release the ODBC statement handle.
Sample code:
SQLHSTMT stmt; rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &stmt); ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode); rcode = SQLExecDirect(stmt, (SQLCHAR*)"CREATE TABLE test_tbl1(id NUMBER PRIMARY KEY, name VARCHAR2(50),age NUMBER NOT NULL)", SQL_NTS); ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode); SQLFreeHandle(SQL_HANDLE_STMT, stmt);Insert data.
Use the
SQLAllocHandlefunction to allocate an ODBC statement handle, and use theSQLExecDirectfunction to execute an SQL statement to insert data into the database table. The code also uses theASSERT_CHECKfunction to check the results of the handle allocation and statement execution to ensure the operations are successful. Finally, use theSQLFreeHandlefunction to release the statement handle and free the resources. The specific steps are as follows:Use the
SQLAllocHandlefunction to allocate an ODBC statement handle.Use the
ASSERT_CHECKfunction to check the return valuercodeof theSQLAllocHandlefunction to ensure that the handle allocation is successful.Use the
SQLExecDirectfunction to execute an SQL statement. In this case, it executes anINSERT INTOstatement to insert three records into thetest_tbl1table.Use the
ASSERT_CHECKfunction to check the return valuercodeof theSQLExecDirectfunction to ensure that the statement execution is successful.Use the
SQLFreeHandlefunction to release the ODBC statement handle.
Sample code:
rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &stmt); ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode); rcode = SQLExecDirect(stmt, (SQLCHAR*)"INSERT INTO test_tbl1 (id,name,age) VALUES (1,'Tom', 18),(2,'Jerry', 20),(3,'Bob', 22)", SQL_NTS); ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode); SQLFreeHandle(SQL_HANDLE_STMT, stmt);Query data.
Use the
SQLAllocHandlefunction to allocate an ODBC statement handle, and use theSQLExecDirectfunction to execute an SQL statement to obtain the result set. Then, use theSQLBindColfunction to bind the columns of the result set to variables, and use theSQLFetchfunction to retrieve the result set row by row. In thewhileloop, print the data in the result set based on the value ofrcode. Finally, use theSQLFreeHandlefunction to release the statement handle and free the resources. The specific steps are as follows:Use the
SQLAllocHandlefunction to allocate an ODBC statement handle.Use the
ASSERT_CHECKfunction to check the return valuercodeof theSQLAllocHandlefunction to ensure that the handle is successfully allocated.Use the
SQLExecDirectfunction to execute an SQL statement. In this case, it executes aSELECTstatement to retrieve all rows from thetest_tbl1table.Use the
ASSERT_CHECKfunction to check the return valuercodeof theSQLExecDirectfunction to ensure that the statement is successfully executed.Declare a variable
resof theSQLLENtype to store the result of the column binding operation.Declare two variables
idandageof theSQLINTEGERtype to store the values of the corresponding columns in the result set.Declare an array
nameof theSQLCHARtype to store the values of the corresponding columns in the result set.Use the
SQLBindColfunction to bind the first column of the result set to the variableid.Use the
SQLBindColfunction to bind the second column of the result set to the arrayname.Use the
SQLBindColfunction to bind the third column of the result set to the variableage.Use the
SQLFetchfunction to retrieve rows from the result set and store the data in the bound variables. Thewhileloop continues to execute until all rows are retrieved.In the
whileloop, print the data of the corresponding row by checking the value ofrcode. IfrcodeisSQL_ERROR, print the error message.Use the
SQLFreeHandlefunction to release the ODBC statement handle.
Code:
rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &stmt); ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode); rcode = SQLExecDirect(stmt, (SQLCHAR*)"SELECT * FROM test_tbl1", SQL_NTS); ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode); SQLLEN res = SQL_NTS; SQLINTEGER id, age; SQLCHAR name[255]; SQLBindCol(stmt, 1, SQL_C_SLONG, &id, sizeof(id), &res); SQLBindCol(stmt, 2, SQL_C_CHAR, name, sizeof(name), &res); SQLBindCol(stmt, 3, SQL_C_SLONG, &age, sizeof(age), &res); while ((rcode = SQLFetch(stmt)) != SQL_NO_DATA_FOUND) { if (rcode == SQL_ERROR) { printf("sql error!\n"); } else { printf("id:%d, name:%s, age:%ld\n", id, name, age); } } SQLFreeHandle(SQL_HANDLE_STMT, stmt);Clean up and release the specific resource handle.
Disconnect from the database and release the related ODBC and environment handles to free resources. By calling the SQLDisconnect and SQLFreeHandle functions, the program can properly close the connection to the database and release the related handles. Finally, returning 0 indicates that the program executed successfully. The specific steps are as follows:
Use the SQLDisconnect function to disconnect from the database.
Use the SQLFreeHandle function to release the database connection handle.
Use the SQLFreeHandle function to release the ODBC environment handle.
Return 0 to indicate that the program executed successfully.
Code:
SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc); SQLFreeHandle(SQL_HANDLE_ENV, henv); return 0;
Complete code
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
static void odbc_print_error(SQLSMALLINT HandleType, SQLHANDLE Handle)
{
SQLCHAR SQLState[6];
SQLINTEGER NativeError;
SQLCHAR SQLMessage[SQL_MAX_MESSAGE_LENGTH] = { 0 };
SQLSMALLINT TextLengthPtr;
SQLGetDiagRec(HandleType, Handle, 1, SQLState, &NativeError, SQLMessage, SQL_MAX_MESSAGE_LENGTH, &TextLengthPtr);
fprintf(stdout, "[%s] (%d) %s\n", SQLState, NativeError, SQLMessage);
}
static void ASSERT_CHECK(SQLSMALLINT HandleType, SQLHANDLE Handle, SQLRETURN rcode)
{
if (rcode != SQL_SUCCESS && rcode != SQL_SUCCESS_WITH_INFO) {
odbc_print_error(HandleType, Handle);
assert(0);
}
}
int main()
{
HENV henv;
SQLCHAR OutConnStr[255];
SQLSMALLINT OutConnStrLen;
SQLRETURN rcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
ASSERT_CHECK(SQL_HANDLE_ENV, henv, rcode);
rcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
ASSERT_CHECK(SQL_HANDLE_ENV, henv, rcode);
SQLHDBC hdbc;
rcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ASSERT_CHECK(SQL_HANDLE_DBC, hdbc, rcode);
char* mydriver = (char*)"Driver={OceanBase ODBC 2.0 Driver};Server=your_ip;Port=your_port;Database=your_schema;User=your_use;Password=your_password;Option=3;";
rcode = SQLDriverConnect(hdbc, NULL, (SQLCHAR*)mydriver, strlen((char*)mydriver) + 1, OutConnStr, 255, &OutConnStrLen, SQL_DRIVER_NOPROMPT);
ASSERT_CHECK(SQL_HANDLE_DBC, hdbc, rcode);
SQLHSTMT stmt;
rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &stmt);
ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode);
rcode = SQLExecDirect(stmt, (SQLCHAR*)"CREATE TABLE test_tbl1(id NUMBER PRIMARY KEY, name VARCHAR2(50),age NUMBER NOT NULL)", SQL_NTS);
ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &stmt);
ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode);
rcode = SQLExecDirect(stmt, (SQLCHAR*)"INSERT INTO test_tbl1 (id,name,age) VALUES (1,'Tom', 18),(2,'Jerry', 20),(3,'Bob', 22)", SQL_NTS);
ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode);
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
rcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &stmt);
ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode);
rcode = SQLExecDirect(stmt, (SQLCHAR*)"SELECT * FROM test_tbl1", SQL_NTS);
ASSERT_CHECK(SQL_HANDLE_STMT, stmt, rcode);
SQLLEN res = SQL_NTS;
SQLINTEGER id, age;
SQLCHAR name[255];
SQLBindCol(stmt, 1, SQL_C_SLONG, &id, sizeof(id), &res);
SQLBindCol(stmt, 2, SQL_C_CHAR, name, sizeof(name), &res);
SQLBindCol(stmt, 3, SQL_C_SLONG, &age, sizeof(age), &res);
while ((rcode = SQLFetch(stmt)) != SQL_NO_DATA_FOUND)
{
if (rcode == SQL_ERROR) {
printf("sql error!\n");
}
else {
printf("id:%d, name:%s, age:%ld\n", id, name, age);
}
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
return 0;
}
References
For more information about how to connect to OceanBase Cloud, see Overview of connection methods.
For more information about OceanBase Connector/ODBC, see OceanBase Connector/ODBC.