I found a lot of information about using MySQL C APIs and examples on the Internet. It's not for VC, it's for BCB, or for Linux, and I 've always wanted to use standard C ++ for something in WIN (the nickname is portability ). So I want to use MySQL C API to try to connect to MySQL under Dev-CPP. The following describes the experiences and connection and configuration methods.
Your ad here
1. Of course, you must first install the MySQL database. I installed 5.0
2. My Dev-CPP version is 4.9.9.2 and the operating system is Windows XP SP2.
A) download the MySQL package for Dev-CPP. There are two methods:
1) Use the dev-CPP update function. "Tool"-> "check for updates" 1
Figure 1
Figure 2
In select devpak server, select devpaks.org, as shown in figure 3.
Figure 3
Click "check for updates" and select "libmysql" from the file list"
Figure 4
Others are downloaded and installed. (Figure 5-7)
Figure 5
Figure 6
Figure 7
B) download the package directly to devpaks.org and install it using Package Manager.
Figure 8
3. Now you can create a project to use the c api provided by MySQL in your own program.
* Note: make the following settings for the project:
1) "project"-> "Project Properties", "Parameters" tab
Add "compiler" and "c ++ compiler"
"-Wall" and "-W" do not include "", and "-lmysql" does not include "" in "connector".
9
Figure 9
2) add in the program
# Include <MySQL/MySQL. h>
The following is a simple example program. # Include <windows. h>
# Include <iostream>
# Include <MySQL/MySQL. h>
# Define select_query "select * from Stu"
Using namespace STD;
Int main (INT argc, char * argv [])
...{
// Connection Params
Char * Host = "localhost ";
Char * user = "root ";
Char * pass = "123 ";
Char * DB = "mydb ";
// Sock
MySQL * sock;
Mysql_res * res;
Sock = mysql_init (0 );
If (sock) cout <"Sock handle OK! "<Endl;
Else ...{
Cout <"Sock handle failed! "<Endl;
Return exit_failure;
}
// Connection
If (mysql_real_connect (sock, host, user, pass, DB, 0, null, 0 ))
Cout <"connection OK! "<Endl;
Else ...{
Cout <"connection failed! "<Endl;
Return exit_failure;
}
// Query
If (mysql_query (sock, select_query ))
...{
Cout <"query failed" <mysql_error (sock) <Endl;
Exit (1 );
}
If (! (RES = mysql_store_result (sock )))
...{
Cout <"couldn't get result from" <mysql_error (sock) <Endl;
Exit (1 );
}
Mysql_field * field;
Mysql_row row;
While (field = mysql_fetch_field (RES )))
...{
Printf ("field name % s", field-> name );
}
While (ROW = mysql_fetch_row (RES ))
...{
Cout <row [0] <"" <row [1] <Endl;
}
System ("pause ");
// Closing connection
Mysql_free_result (RES );
Mysql_close (sock );
Return exit_success;
}
Running result