How to connect MySQL with C + + in VS

Source: Internet
Author: User
Tags error handling goto

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)

    1. In the project Properties "VC + + Directory" → "Include directory", select the MySQL include folder;
    2. "Library Directory" Select the Lib folder of MySQL;
    3. "linker" → "input" Additional dependencies Add "Libmysql.lib".
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.