[c/c++] c 操作mysql数据库
输出mysql版本
1 #include <my_global.h> 2 #include <mysql.h> 3 #include <stdlib.h> 4 #include <stdio.h> 5 6 int main(int argc,char **argv) { 7 printf("mysql client version:%s\n",mysql_get_client_info()); 8 return 0; 9 }
编译
gcc version.c -o version $(mysql_config --cflags --libs)
结果
$ ./version mysql client version:5.1.63
创建DB
1 #include <my_global.h> 2 #include <mysql.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main(int argc,char **argv) { 7 MYSQL *conn; 8 9 conn = mysql_init(NULL); 10 if(conn == NULL) { 11 printf("Error %u:%s\n",mysql_errno(conn),mysql_error(conn)); 12 exit(EXIT_FAILURE); 13 } 14 15 // host user password 16 if(mysql_real_connect(conn,"localhost","root","admin",NULL,0,NULL,0) == NULL) { 17 printf("Error %u:%s\n",mysql_errno(conn),mysql_error(conn)); 18 exit(EXIT_FAILURE); 19 } 20 21 char* sql = "CREATE DATABASE IF NOT EXISTS test_cdb"; 22 //char* sql = "CREATE database test_cdb"; 23 if(mysql_query(conn,sql)) { 24 printf("Error %u:%s\n",mysql_errno(conn),mysql_error(conn)); 25 exit(EXIT_FAILURE); 26 } 27 28 mysql_close(conn); 29 30 exit(EXIT_SUCCESS); 31 }
创建Table
1 #include <my_global.h> 2 #include <mysql.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main(int argc,char **argv) { 7 MYSQL *conn; 8 9 conn = mysql_init(NULL); 10 // host user password dbname 11 if(mysql_real_connect(conn,"localhost","root","admin","test_cdb",0,NULL,0) == NULL) { 12 printf("Error %u:%s",mysql_errno(conn),mysql_error(conn)); 13 exit(EXIT_FAILURE); 14 } 15 16 char* sql = "CREATE TABLE IF NOT EXISTS test(name VARCHAR(25));"; 17 if(mysql_query(conn,sql)) { 18 printf("Error %u:%s",mysql_errno(conn),mysql_error(conn)); 19 exit(EXIT_FAILURE); 20 } 21 sql = "INSERT INTO test VALUES('test1')"; 22 if(mysql_query(conn,sql)) { 23 printf("Error %u:%s",mysql_errno(conn),mysql_error(conn)); 24 exit(EXIT_FAILURE); 25 } 26 27 mysql_close(conn); 28 exit(EXIT_SUCCESS); 29 }
查询
1 #include <my_global.h> 2 #include <mysql.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main(int argc,char **argv) { 7 MYSQL *conn; 8 MYSQL_RES *result; 9 MYSQL_ROW row; 10 MYSQL_FIELD *field; 11 12 int num_fields; 13 int i; 14 int j = 0; 15 16 conn = mysql_init(NULL); 17 if(mysql_real_connect(conn,"localhost","root","admin","test_cdb",0,NULL,0) == NULL) { 18 printf("Error %u:%s",mysql_errno(conn),mysql_error(conn)); 19 exit(EXIT_FAILURE); 20 } 21 22 char* sql = "SELECT * FROM test"; 23 if(mysql_query(conn,sql)) { 24 printf("Error %u:%s",mysql_errno(conn),mysql_error(conn)); 25 exit(EXIT_FAILURE); 26 } 27 result = mysql_store_result(conn); 28 num_fields = mysql_num_fields(result); // 记录项数 29 30 while((row = mysql_fetch_row(result))) { 31 // for(int i = 0; i < num_fields;i++) { // allowed c99 mode 32 for(i = 0; i < num_fields;i++) { 33 if(j == 0) { 34 // struct ? 35 while(field = mysql_fetch_field(result)) { 36 printf("%s ",field->name); 37 } 38 printf("\n"); 39 } 40 printf("%s ",row[i]? row[i] : "NULL"); 41 } 42 printf("\n"); 43 j++; 44 } 45 mysql_free_result(result); 46 47 mysql_close(conn); 48 exit(EXIT_SUCCESS); 49 }