C 연결 코드

4634 단어 코드
 #include <mysql.h>/*          */
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 /*              */
 #define HOST "localhost"
 #define USERNAME "ABitNo"
 #define PASSWORD "ABitNo"
 #define DATABASE "abitno"
 
 /*           sql  */
 void exe_sql(char* sql) {
 
     MYSQL my_connection; /*         */
     int res; /*  sql        */
 
     /*   mysql  my_connection*/
     mysql_init(&my_connection);
 
     /*      mysql.h      ,             mysql  , 
          ,             */
     if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE,
             0, NULL, CLIENT_FOUND_ROWS)) {/*    */
 
         printf("     exe_sql    !
"); /* utf8, */ mysql_query(&my_connection, "set names utf8"); /* mysql_query sql , int , 0, */ res = mysql_query(&my_connection, sql); if (res) {/* */ printf("Error: mysql_query !
"); /* */ mysql_close(&my_connection); } else {/* */ /*mysql_affected_rows sql */ printf("%d !

", mysql_affected_rows(&my_connection)); /* */ mysql_close(&my_connection); } } else { /* */ printf(" exe_sql !
"); } } /* sql , */ void query_sql(char* sql) { MYSQL my_connection; /* */ int res; /* sql */ MYSQL_RES *res_ptr; /* */ MYSQL_FIELD *field; /* */ MYSQL_ROW result_row; /* */ int row, column; /* */ int i, j; /* */ /* mysql my_connection*/ mysql_init(&my_connection); /* mysql.h , mysql , , */ if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) {/*Connection success*/ printf(" query_sql !
"); /* utf8, */ mysql_query(&my_connection, "set names utf8"); /* mysql_query sql , int , 0, */ res = mysql_query(&my_connection, sql); if (res) { /* */ printf("Error: mysql_query !
"); /* */ mysql_close(&my_connection); } else { /* */ /* res_ptr*/ res_ptr = mysql_store_result(&my_connection); /* , print*/ if (res_ptr) { /* */ column = mysql_num_fields(res_ptr); row = mysql_num_rows(res_ptr) + 1; printf(" %lu
", row); /* */ for (i = 0; field = mysql_fetch_field(res_ptr); i++) printf("%s\t", field->name); printf("
"); /* */ for (i = 1; i < row; i++) { result_row = mysql_fetch_row(res_ptr); for (j = 0; j < column; j++) printf("%s\t", result_row[j]); printf("
"); } } /* */ mysql_close(&my_connection); } } } int main(int argc, char *argv[]) { /* */ char *exe = "insert into abitno values('ABitNo','http://ABitNo.LinPie.com');"; exe_sql(exe); /* */ char *query = "select * from abitno;"; query_sql(query); return 0; }

 http://abitno.me/linux-c-connect-mysql.html

좋은 웹페이지 즐겨찾기