1. Add header file path (include path in MySQL installation path)
2, add the storage file (directly from the MySQL installation path copy libmysql.lib can)
3. Programming Operation Database
Code
AccessToMySQL.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <Windows.h> #include <mysql.h> #pragma comment (lib, "Libmysql.lib") MySQL MySQL mysql_res* result; Mysql_row Row;int Main (void) {//init the MySQL parametermysql_init (&mysql);//connect the database if (!mysql_real_ Connect (&mysql, "127.0.0.1", "Root", "111", "MyTest", 3306,null,0)) {printf (mysql_error (&mysql));p rintf ("\ Ncannot access to the database!!! \ n "); system (" pause "); exit (-1);} Construct the query SQL statementschar* sql= "select * from student where name= '"; char dest[100]={""};strcat (dest,sql);p rintf ("Please enter the student name:"); char name[10]={""};gets (name); strcat (dest,name); Strcat (dest, "'");//excute The SQL statementsif (mysql_query (&mysql,dest)) {printf ("Cannot access the database with excuting \"%s\ ".", dest); System ("pause"); exit (-1);} Deal with the Resultresult=mysql_store_result (&mysql), if (mysql_num_rows (result)) {while (Row=mysql_fetch_row (result))) {printf ("%s\t%s\t%s\n", Row[0],row[1],row[2]);}} Release the Resourcemysql_free_result (result); Mysql_close (&mysql); System ("pause"); return 0;}
C language access to MySQL database method