整理旧资料,收拾新东西,有些db状态不是mysql客户端能模拟出来的,所以记录下打样过程,如果以后有需要也不用从头看起
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h> MYSQL *conn; void print_err() { fprintf(stderr, "%d %s", mysql_errno(conn),mysql_error(conn)); } void init_db() { conn = mysql_init(NULL); if(conn == NULL) { fprintf(stderr, "mysql_init() failed"); exit(EXIT_FAILURE); } mysql_real_connect(conn, "192.168.1.10", "test1234", "test1234", "db1", 3306, NULL, 0); } void query(char* sql, char* param1) { int ret = -1; MYSQL_STMT *stmt; MYSQL_BIND bind[1]; long length; stmt = mysql_stmt_init(conn); if(conn == NULL) { fprintf(stderr, "mysql_stmt_init() failed\n"); exit(EXIT_FAILURE); } ret = mysql_stmt_prepare(stmt, sql, strlen(sql)); if(ret != 0) { fprintf(stderr, "mysql_stmt_prepare() failed\n"); print_err(); exit(EXIT_FAILURE); mysql_stmt_execute(stmt); }else{ fprintf(stdout, "mysql_stmt_prepare() success\n"); } if(mysql_stmt_reset(stmt)) { fprintf(stderr, "mysql_stmt_reset() failed\n"); print_err(); }else{ fprintf(stdout, "mysql_stmt_reset() success\n"); } ret = mysql_stmt_prepare(stmt, sql, strlen(sql)); if(ret != 0) { fprintf(stderr, "mysql_stmt_prepare() failed\n"); print_err(); exit(EXIT_FAILURE); mysql_stmt_execute(stmt); }else{ fprintf(stdout, "mysql_stmt_prepare() success\n"); } memset(bind, 0, sizeof(bind)); /*普通bind*/ bind[0].buffer_type = MYSQL_TYPE_STRING; bind[0].buffer = (char *)param1; bind[0].buffer_length = strlen(param1); ret = mysql_stmt_bind_param(stmt, bind); if(ret != 0) { fprintf(stderr, "mysql_stmt_bind_param() failed\n"); exit(EXIT_FAILURE); mysql_stmt_execute(stmt); }else{ fprintf(stdout, "mysql_stmt_bind_param() success\n"); } /*发送long data*/ // bind[0].buffer_type= MYSQL_TYPE_STRING; // bind[0].length= &length; // bind[0].is_null= 0; // ret = mysql_stmt_bind_named_param(stmt, bind, 1, NULL); // if(ret != 0) // { // fprintf(stderr, "mysql_stmt_bind_named_param() failed\n"); // print_err(); // }else{ // fprintf(stdout, "mysql_stmt_bind_named_param() success\n"); // } if(mysql_stmt_send_long_data(stmt,0,"MySQL",5)) { fprintf(stderr, "mysql_stmt_send_long_data() failed\n"); print_err(); }else{ fprintf(stdout, "mysql_stmt_send_long_data() success\n"); } if(mysql_stmt_send_long_data(stmt,0," - The most popular Open Source database",40)) { fprintf(stderr, "mysql_stmt_send_long_data() failed\n"); print_err(); }else{ fprintf(stdout, "mysql_stmt_send_long_data() success\n"); } /***/ ret = mysql_stmt_execute(stmt); if(ret != 0) { fprintf(stderr, "mysql_stmt_execute() failed\n"); print_err(); }else{ fprintf(stdout, "mysql_stmt_execute() success\n"); } mysql_stmt_close(stmt); } void close_db() { mysql_close(conn); } int main() { init_db(); query("select md5(?) from dual", "123"); close_db(); } /* gcc -o foo mysql_c_demo.c -I/home/mysql8/mysql8.0.22_run/include -L/home/mysql8/mysql8.0.22_run/lib -lmysqlclient */ |