MYSQL C API : struct MYSQL_STMT 結構的組合使用

來源:互聯網
上載者:User

標籤:

 

 1 #include <iostream> 2 #include <string> 3  4 #include <string.h> 5 #include <assert.h> 6  7 #include <mysql.h> 8  9 static void do_stmt_sql(MYSQL *ms_conn);10 11 int main()12 {13     // 初始化MYSQL 執行個體14     MYSQL *ms_conn = mysql_init(NULL);15     if (ms_conn == NULL)16     {17         std::cout << "mysql init failed." << std::endl;18         return 0;19     }20     std::cout << "mysql init successful." << std::endl;21 22     // 串連到MYSQL 伺服器23     MYSQL *ms_ret = mysql_real_connect(ms_conn, "localhost", "suyh", 24             "suyunhong", "suyh_db", 0, NULL, 0);25     if (ms_ret == NULL)26     {27         std::cout << "mysql connect failed. " 28             << mysql_error(ms_conn) << std::endl;29         mysql_close(ms_conn), ms_conn = NULL;30         return 0;31     }32     std::cout << "mysql connect successful." << std::endl;33 34     do_stmt_sql(ms_conn);35 36     // 釋放資源37     mysql_close(ms_conn), ms_conn = NULL;38     return 0;39 }40 41 static void do_stmt_sql(MYSQL *ms_conn)42 {43     assert(ms_conn != NULL);44     if (ms_conn == NULL)45         return ;46 47     MYSQL_STMT *stmt = NULL;48     stmt = mysql_stmt_init(ms_conn);49     if (stmt == NULL)50     {51         std::cout << "stmt is NULL. mysql_stmt_init failed. "52             << mysql_error(ms_conn) << std::endl;53         return ;54     }55     std::cout << "MYSQL_STMT init successful." << std::endl;56 57     const char str_sql[] = "INSERT INTO tb_bin_data(bin_data) VALUES(?)";58 59     int res = 0;60     res = mysql_stmt_prepare(stmt, str_sql, sizeof(str_sql) - 1);61     if (res != 0)62     {63         std::cout << "mysql_stmt_prepare INSERT failed."64             << mysql_stmt_error(stmt) << std::endl;65         return ;66     }67 68     // 待存到MYSQL 的位元據69     char bin_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};70 71     MYSQL_BIND bind[1];72     memset(bind, 0, sizeof(bind));73     bind[0].buffer_type = MYSQL_TYPE_BLOB;74     bind[0].is_null = NULL;75     bind[0].buffer = bin_data;76     bind[0].buffer_length = sizeof(bin_data);77 78     res = mysql_stmt_bind_param(stmt, bind);79     if (res != 0)80     {81         std::cout << "mysql_stmt_bind_param failed. " 82             << mysql_stmt_error(stmt) << std::endl;83         mysql_stmt_close(stmt), stmt = NULL;84         return ;85     }86     std::cout << "mysql_stmt_bind_param successful." << std::endl;87 88     // res = mysql_stmt_send_long_data(stmt, 0, escape_bin, strlen(escape_bin));89     // std::cout << "mysql_stmt_send_long_data result is " << res << std::endl;90 91     res = mysql_stmt_execute(stmt);92     std::cout << "mysql_stmt_execute() func result is " << res << std::endl;93 94     mysql_stmt_close(stmt), stmt = NULL;95 }

 

MYSQL C API : struct MYSQL_STMT 結構的組合使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.