VisualStudio2013でMySQLの勉強をしています。
PreparedStatementを使ったSELECTクエリのテストのため下記のコードを作りました。
参考にしたサイト:
http://www.erickcantwell.com/2011/08/mysql-prepared-statements-in-c/
http://d.hatena.ne.jp/pyopyopyo/20091219/p1
lang
1#include <winsock.h> 2#include <mysql.h> 3#pragma comment(lib,"libmysql.lib") 4#include <stdio.h> 5 6const char* DB_HOST = "localhost"; 7const char* DB_USER = "user-name"; 8const char* DB_PASS = "password"; 9const char* DB_NAME = "test"; 10const unsigned long PORT_NUMBER = 3306; 11const char* TABLE_NAME = "test"; 12 13const int STRING_SIZE = 256; 14 15int main() { 16 MYSQL* mysql; 17 MYSQL_STMT* stmt; 18 19 char account[STRING_SIZE]; //stmtに渡すパラメータ 20 char buf[STRING_SIZE]; //データの受け取り先 21 unsigned long str_length; 22 const char* query = "SELECT * FROM test WHERE account = ?"; //クエリ 23 24 printf_s("Start\n"); 25 26 ///初期化と接続//////////////////////////////////////////////////////////////// 27 if ((mysql = mysql_init(NULL)) == NULL) { 28 printf_s("ERROR: MYSQL_INITIALIZE finish program\n"); //初期化失敗 29 return -1; 30 } 31 32 if (mysql_real_connect(mysql, DB_HOST, DB_USER, DB_PASS, DB_NAME, PORT_NUMBER, NULL, 0) == NULL) { 33 printf_s("ERROR: MYSQL_CONNECT :%s\n", mysql_error(mysql)); //接続失敗 34 return -1; 35 } else { 36 printf_s("CONNECT_SUCCESS!\n"); //接続成功 37 } 38 /////////////////////////////////////////////////////////////////////////////// 39 40 //PreparedStatementの作成・準備/////////////////////////////////////////////// 41 if ((stmt = mysql_stmt_init(mysql)) == NULL) { 42 printf_s("ERROR: MYSQL_STMT_INIT :%s\n", mysql_error(mysql)); 43 return -1; 44 } 45 46 if (mysql_stmt_prepare(stmt, query, strlen(query)) != 0) { 47 printf_s("ERROR: MYSQL_STMT_PREPARE :%s\n", mysql_stmt_error(stmt)); 48 return -1; 49 } 50 /////////////////////////////////////////////////////////////////////////////// 51 52 //パラメータの設定///////////////////////////////////////////////////////////// 53 MYSQL_BIND param[1]; 54 param[0].buffer_type = MYSQL_TYPE_STRING; 55 param[0].buffer = &account[0]; 56 param[0].buffer_length = STRING_SIZE; 57 param[0].is_null = 0; 58 param[0].length = &str_length; 59 60 if (mysql_stmt_bind_param(stmt, param) != 0) { 61 printf_s("ERROR: MYSQL_STMT_BIND_PARAM :%s\n", mysql_stmt_error(stmt)); 62 return -1; 63 } 64 /////////////////////////////////////////////////////////////////////////////// 65 66 //出力先の設定///////////////////////////////////////////////////////////////// 67 MYSQL_RES* res = mysql_stmt_result_metadata(stmt); 68 int num_fields = mysql_num_fields(res); 69 mysql_free_result(res); 70 71 //MYSQL_BIND* result = new MYSQL_BIND[num_fields]; 72 MYSQL_BIND result[1]; 73 result[0].buffer_type = MYSQL_TYPE_STRING; 74 result[0].buffer = &buf[0]; 75 result[0].buffer_length = STRING_SIZE; 76 result[0].is_null = 0; 77 result[0].length = &str_length; 78 79 if (mysql_stmt_bind_result(stmt, result) != 0) { 80 printf_s("ERROR: MYSQL_STMT_BIND_RESULT :%s\n", mysql_stmt_error(stmt)); //ここでエラーがでる 81 return -1; 82 } 83 printf_s("OK: MYSQL_STMT_BIND_RESULT"); 84 /////////////////////////////////////////////////////////////////////////////// 85 86 //実行と結果の取得・解析/////////////////////////////////////////////////////// 87 strcpy_s(account, "admin"); 88 str_length = strlen(account); 89 if (mysql_stmt_execute(stmt)) { 90 printf_s("ERROR: MYSQL_STMT_EXECUTE :%s\n", mysql_stmt_error(stmt)); 91 return -1; 92 } 93 printf_s("OK: MYSQL_STMT_EXECUTE"); 94 95 strcpy_s(account, "hoge"); 96 str_length = strlen(account); 97 if (mysql_stmt_execute(stmt)) { 98 printf_s("ERROR: MYSQL_STMT_EXECUTE :%s\n", mysql_stmt_error(stmt)); 99 return -1; 100 } 101 printf_s("OK: MYSQL_STMT_EXECUTE"); 102 103 104 int i = -1; 105 while ((i = mysql_stmt_fetch(stmt)) == 0) { 106 printf("output: %s\n", buf); 107 } 108 if (i != MYSQL_NO_DATA) { 109 printf_s("ERROR: MYSQL_STMT_FETCH :%s\n", mysql_stmt_error(stmt)); 110 return -1; 111 } 112 /////////////////////////////////////////////////////////////////////////////// 113 114 //Close the statement 115 if (mysql_stmt_close(stmt) != 0) { 116 printf_s("ERROR: MYSQL_STMT_CLOSE :%s\n", mysql_stmt_error(stmt)); 117 return -1; 118 } 119 120 // 切断 121 mysql_close(mysql); 122 123 printf_s("Fin\n"); 124 return 0; 125}
接続は成功するのですが、
mysql_stmt_bind_result()で「Using unsupported buffer type: 252 (parameter 2)」というエラーがでます。
MYSQL_TYPE_STRING=254にもかかわらず252だとエラーが出ています。
MYSQL_TYPE_STRINGをMYSQL_TYPE_VAR_STRINGに変えて実行しても同じエラーがでました。
C++は普段良く使いますが、MySQLは以前PHPから少し触った程度です。
PreparedStatementを使うのは初めてです。
開発環境
- OS: Windows
- IDE: VisualStudio2013
- 開発言語: C++
- MySQLのバージョン: MySQL Server 5.5
- 利用サーバー:ローカル(XAMPP)
宜しくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。