voidgetdata() { MYSQL_RES * mysql_res; MYSQL_FIELD * mysql_field; MYSQL_ROW mysql_row; char * sql="select * from virus_data where city_deadCount>50";
if(mysql_query(&mysqlconn,sql)==0) { cout<<"get data success"<<endl; mysql_res=mysql_store_result(&mysqlconn); if(mysql_res) { int fiendcount=mysql_num_fields(mysql_res); int rowcount=mysql_num_rows(mysql_res); cout<<fiendcount<<endl; cout<<rowcount<<endl;
Navigate to the /bin directory using an Administrator command line, then enter mysqld.exe --install&&net start mysql.
Changing Password
For the first-time password change, enter mysqladmin -u root password "new_password". To change an existing password, enter mysqladmin -u root -p password "new_password", then enter the original old password.
IDE Setup
First, add mysql5\include to the include directories. Add mysql5\lib\opt to the lib directories. (Optional) In the linker’s additional libraries, add libmysql.lib. If you cannot find how to add libmysql.lib, you can skip this step.
Using C++ Code to Connect and Operate the Database
Required Header Files
First, include the following header files.
1 2 3
#include<winsock2.h> #include<mysql.h> #pragma comment(lib,"libmysql.lib") // If you have added libmysql.lib in your IDE settings, this line is not necessary, but including it is fine.
Connecting to the Database
1 2 3 4 5 6 7 8 9 10 11
MYSQL mysqlconn; mysql_init(&mysqlconn); char * host="127.0.0.1"; // Server address int port=3306; // Port char * username="root"; // Username char * password="1234567890"; // Password char * dbname="virus"; // Database name
voidgetdata() { MYSQL_RES * mysql_res; MYSQL_FIELD * mysql_field; MYSQL_ROW mysql_row; char * sql="select * from virus_data where city_deadCount>50";
if(mysql_query(&mysqlconn,sql)==0) { cout<<"get data success"<<endl; mysql_res=mysql_store_result(&mysqlconn); if(mysql_res) { int fiendcount=mysql_num_fields(mysql_res); int rowcount=mysql_num_rows(mysql_res); cout<<fiendcount<<endl; cout<<rowcount<<endl;