· 5 years ago · May 04, 2020, 06:52 PM
1 MYSQL* mysql_server = mysql_init(NULL);
2
3 // Connnect to mysql
4 if (!mysql_real_connect(mysql_server, "127.0.0.1", "root", "root", NULL, 6033, NULL, 0)) {
5 diag("%s", err_msg(mysql_error(mysql_server), __FILE__, __LINE__).c_str());
6 return -1;
7 }
8
9 const char* drop_test_table = "DROP TABLE IF EXISTS sysbench.prep_stmt_test_table";
10 const char* create_test_table =
11 "CREATE TABLE sysbench.prep_stmt_test_table ( "
12 "test_elem INT NOT NULL)";
13 const char* insert_into_test_table =
14 "INSERT INTO sysbench.prep_stmt_test_table "
15 "( test_elem ) VALUES "
16 "( 1 )";
17
18 MYSQL_QUERY(mysql_server, drop_test_table);
19 MYSQL_QUERY(mysql_server, create_test_table);
20 MYSQL_QUERY(mysql_server, insert_into_test_table);
21
22 MYSQL_STMT* stmt = mysql_stmt_init(mysql_server);
23
24 MYSQL_BIND bind[1];
25 memset(bind, 0, sizeof(bind));
26
27 int data = 1;
28 my_bool is_null = 0;
29
30 const char* stmt_query = "select * from sysbench.prep_stmt_test_table where test_elem = ?";
31 int stmt_error = mysql_stmt_prepare(stmt, stmt_query, std::strlen(stmt_query));
32 if (stmt_error) {
33 const std::string t_bind_err = "stmt preparation failed - %d";
34 std::string bind_err {};
35 string_format(t_bind_err, bind_err, mysql_stmt_errno(stmt));
36 diag("%s", err_msg(bind_err, __FILE__, __LINE__).c_str());
37
38 res = -1;
39 goto cleanup;
40 }
41
42 // Initialize bind parameter
43 bind[0].buffer_type = MYSQL_TYPE_SHORT;
44 bind[0].buffer = reinterpret_cast<char*>(&data);
45 bind[0].is_null = reinterpret_cast<char*>(&is_null);
46 bind[0].length = 0;
47
48 if (mysql_stmt_bind_param(stmt, bind)) {
49 const std::string t_bind_err = "stmt binding failed - %d";
50 std::string bind_err {};
51 string_format(t_bind_err, bind_err, mysql_stmt_errno(stmt));
52 diag("%s", err_msg(bind_err, __FILE__, __LINE__).c_str());
53
54 res = -1;
55 goto cleanup;
56 }
57
58 stmt_error = mysql_stmt_execute(stmt);
59 if (stmt_error) {
60 std::string t_exec_error = "stmt execution failed - %d";
61 std::string exec_error {};
62 string_format(t_exec_error, exec_error, mysql_stmt_errno(stmt));
63 diag("%s", err_msg(exec_error, __FILE__, __LINE__).c_str());
64
65 res = -1;
66 goto cleanup;
67 }