C + + Connect MySQL database (using MySQL API mode, Environment vs2013+mysql5.6)

Source: Internet
Author: User

Reprint please indicate source, original address http://www.cnblogs.com/zenki-kong/p/4382657.html

Just started blogging, Bo Master is just a junior Wang, not fine, if there are mistakes also please predecessors pointed (>^ω^<), nonsense not much to say, into the text

The next blog should talk about using MySQL connector C + + to let C + + connect MySQL, this article is directly using the MySQL API

First of all, explain the development environment,64-bit mysql5.6.23+vs2013+win7 flagship edition

Start with mysql5.7.6 hint missing binary_log_types.h, search the entire folder did not have this file, so use 5.6 bar

1. To use the MySQL API, need to MySQL directory under the include and Lib two folders, if you use Wamp or Phpstudy, the inside of MySQL may not have these two folders, Then go to the next decompression version of MySQL, I use the 64-bit mysql5.6.23 installation version

2. This is important, so let's talk about this in advance.

Bloggers from the beginning of the card here, said how to compile does not pass it (ㄒoㄒ)//, a variety of external symbols can not be parsed, studied for half a day, the reason is that the database is 64-bit version, and the program platform is 32 (after all, my development experience is not much ...). (>﹏<)). So we have to set up the platform first, if you are a 32-bit database, you can probably skip this step.

Top Corner Active solution Platform Point New

3. Open the project-properties in vs2013, include the Include and Lib two folders, the specific path to see a personal bar, if you are the installation version, in the MySQL server root directory (I was directly installed in the MySQL root directory). If you are included and then change the 64-bit/32-bit platform, it may have to be re-included, so I said the platform problem ahead of time.

Let's see what these two folders have.

And then include it in.

4. Copy the Libmysql.dll in the Lib folder into your project directory (where H and CPP are placed)

5. Out of the way, the end is the code.

First is to include the header files and libraries, I heard that the header file order Exchange will have a problem, you can try, because I project itself very early included winsock2.h, do not try to pull, I am also very lazy (^. ^) y-~~

#include <WinSock2.h>"mysql.h"#pragma comment (lib, " Wsock32.lib ")#pragma comment (lib," Libmysql.lib ")

Then is the main code for testing, of course, before testing you need to build a table, classes and functions such as the use of self-degree Niang check it ... Actually, it's obviously understandable.

Define three variables

*result; Mysql_row ROW;

Then the code for the database operation

Mysql_init (&MySQL); Mysql_real_connect (&mysql,"localhost","Root","Root","DND",3306Null0);Char*sql ="SELECT * from user";//using CString under Unicode can be cumbersome, directly with Charmysql_query (&mysql, SQL); result= Mysql_store_result (&MySQL); while(row = mysql_fetch_row (result))! =NULL) {Showtext (row[1]);//here is a function of the display, the console can be used cout}

The above code, there is not too much error detection, because a few lines of code, there is a problem with the annotation test bar, you can use

Unicode can be cumbersome to use CString, the direct conversion type is not possible (only the first character will be preserved), so the query statement is wrong

By the way, does this sentence cause a memory leak (I certainly know the answer, ha, when it is a beginner, the big God ignores it ha)

Char *sql = "SELECT * from user";

Sql= "I am Zenki Kong"//That add more this sentence? Will it cause a memory leak?

At this point, if no accident on the success!!!

But personally feel that using connector connection is more convenient, so after writing this blog I have started, if successful I will also write blog, interested students can pay attention to.

There are three ways to learn about C + + connection MySQL, ADO, direct use of MySQL API and connector C + +

By the way, how do different connections affect the efficiency of a database? Which one is the most efficient, please?

C + + Connect MySQL database (using MySQL API mode, Environment vs2013+mysql5.6)

Related Article

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.