#include <stdio.h> #include <winsock2.h> #include <mysql.h>/* database connection macros */#define HOST "localhost" # Define USERNAME "root" #define PASSWORD "root" #define DATABASE "MySQL" int main () {char *query; query= "Select Host,user from User"; Query_sql (query); return 0;} void Query_sql (char* sql) {MYSQL my_connection;/* This is a database connection */int res;/* return flag after executing SQL sentence */mysql_res *res_ptr;/* pointing to Pointer to the result of the query */Mysql_field *field; /* Field structure pointer */Mysql_row result_row; /* Query information returned by line */int row, column; /* The number of rows and columns returned by the query */int i, J; /* Initialize MySQL connection my_connection*/mysql_init (&my_connection); /* Establish MySQL connection */if (NULL! = Mysql_real_connect (&my_connection, HOST, USERNAME, PASSWORD, Database, 0, NULL, client_found_rows) */* Connection succeeded */{printf ("DB query Query_sql connection succeeded! \ n "); /* Set the query code to GBK to support the Chinese */mysql_query (&my_connection, "Set names GBK"); res = mysql_query (&my_connection, SQL); if (res)/* Execution failed */{ printf ("Error:mysql_query!\n"); /* Close Connection */Mysql_close (&my_connection); } else/* Now represents the success of the execution */{/* The results of the query to res_ptr*/res_ptr = Mysql_store_result (&my_co Nnection); /* If the result is not empty, put the result print*/if (res_ptr) {/* Gets the number of rows and */column = Mysql_num_f Ields (RES_PTR); row = Mysql_num_rows (res_ptr); printf ("Query to%d lines \ n", row); /* The field name for output results */for (i = 0; field = Mysql_fetch_field (res_ptr); i++) printf ("%10s", field- >name); printf ("\ n"); /* Output results by line */for (i = 1; i < row+1; i++) {Result_row = Mysql_fetch_row ( RES_PTR); for (j = 0; j < column; J + +) printf ("%10s", Result_row[j]); printf ("\ n"); }/* Do not forget to close the connection */ Mysql_close (&my_connection); }} else {printf ("Database connection failed"); }}
<p style= "LINE-HEIGHT:21PX; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " > precautions: </p><p style= "LINE-HEIGHT:21PX; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " >1. Add libmysql.lib path </p><p style= "LINE-HEIGHT:21PX in linker settings->link libraries; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " >2. Add mysql.h folder path </p><p style= "line-height:21px in search directories-> compiler; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " > (in short, let the compiler can find Libmysql.lib and mysql.h) </p><p style= "LINE-HEIGHT:21PX; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " >3. Add # include <winsock2.h> before # include <mysql.h> (there is no such thing as a lot of information on the web, but I found that this is not a compile-time operation) </p> <p style= "LINE-HEIGHT:21PX; margin:10pxAuto Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " ></p><p style=" line-height:21px; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " ></p><p style=" line-height:21px; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " > Operation result: </p><p style= "LINE-HEIGHT:21PX; margin:10px Auto; Font-family:verdana, Geneva, Arial, Helvetica, Sans-serif; font-size:14px; " ></p>
</pre><pre name= "code" class= "CPP" >
C language Connection MySQL (codeblocks)