In such as Peng Online see how to connect MySQL with C, solved the sophomore has been confused, exultation!
First step Download
Installed database is like Peng Network Mysql:http://pan.baidu.com/s/1c0m3xiw extract code: M9SN)
Save in My network disk (MySQL Server 5.6 Free install version. zip): http://pan.baidu.com/s/1jG4KZ8y
Second Step installation
Installation of the green version of MySQL server:
1) Unzip to a folder without Chinese or white space
2) Double-click Mysqld.exe, if there is mysqld.exe in the process is OK
3) The above methods need to be run manually every time you restart the computer, especially if running on the server, you can not start before logging on. You can register as a system service: Run command line as Administrator, CD to the bin folder of MySQL, perform "mysqld-install"; Uninstall service: Mysqld-remove.
* The user name and password for the green version of MySQL are: root
Step three installation management tools
Database management tools (Navicat Lite) V9.1.11
: http://www.cr173.com/soft/44816.html
*navicat and MySQL docking: http://www.rupeng.com/Segments/Index/1800
Fourth Step environment construction
Source (http://www.rupeng.com/Segments/Index/1896)
- In the project Properties "VC + + Directory" → "Include directory", select the MySQL include folder;
- "Library Directory" Select the Lib folder of MySQL;
- "linker" → "input" Additional dependencies Add "Libmysql.lib".
- Compile run, will error can not find "Libmysql.dll", the MySQL libmysql.dll copy to exe directory.
The Fifth Step test
Test code:
#include <stdlib.h><stdio.h><winsock.h>int main () { *mysql = Mysql_init (0); return 0 ;}
View Code
#include <stdlib.h>#include<stdio.h>#include<winsock.h>#include<mysql.h>intMain () {MYSQL*mysql = Mysql_init (0); if(!mysql_real_connect (MySQL,"localhost","Root","Root","study3",0,0,0) {printf ("Error connecting database:%s", Mysql_error (MySQL)); GotoExit//Goto is generally not recommended, but it is very useful in error handling.//There is no absolutely good thing, no absolutely bad thing. Just look at the level of people using this technique .} printf ("Connect to the database successfully! \ n"); if(mysql_query (MySQL,"Set names GBK") {printf ("set connection encoding failed%s", Mysql_error (MySQL)); Gotoexit; } if(mysql_query (MySQL,"INSERT INTO t_users (Username,password) VALUES (' I am Chinese ', ' aaa123 ')") {printf ("insert failed,%s", Mysql_error (MySQL)); Gotoexit; } printf ("Insert succeeded \ n"); Exit:mysql_close (MySQL);//The program must finally close the connection, or there will be too many MySQL server connection to the possibility of deathprintf"Exit"); GetChar (); return 0;}
View Code
No error is the connection success!
Exultation, desire to achieve!!
Source: (http://www.rupeng.com/Segments/Index/1896)
How to connect MySQL with C + + in VS