C + + operation MySQL Introduction detailed

Source: Internet
Author: User
Tags mysql in mysql version

First, depending on your current operating system, there are also development tools to select the appropriate MySQL version. I choose the environment is WIN10 + vs2013

Need to solve three problems: 1, download and install MySQL server, and log in to MySQL test whether the installation is successful, 2, build the MySQL development environment, 3, write a simple demo connection operation database to see if you can succeed.

The following three questions are completed in turn:

1. Download and install MySQL:

Note: Installation environment is a matter of patience and torment, so calm down and slowly get it.

Open https://downloads.mysql.com/archives/This address, find the MySQL installer download, or download this address: https://dev.mysql.com/archives/get/ File/mysql-installer-community-5.7.21.0.msi.

Install MySQL, and test whether the installation is successful, please separate Baidu. Aha, Aha, burp.

Lazy Person Reference: https://www.cnblogs.com/dtting/p/7691202.html

After the installation, you can look at the Task Manager Mysqld.exe this process is not in.

It is possible that you are logged in the CMD interface to execute the MySQL command, you do not have to panic, you can CD to the MySQL installation directory and then perform the directory mysql.exe-u root-p login. Can execute this SQL to see the current database, show databases;

Use the world;

May also encounter a lot of problems, Baidu a bit.

2. Build MySQL development environment:

Congratulations, you've done your first step, you've been great.

In the directory you installed have the development environment you need, see test: https://www.cnblogs.com/magicsoar/p/3817518.html.

Opens the project properties, and the C + + routine adds the MySQL header file path to the Add header file directory (Additional Include directories). Mine is C:\Program files\mysql\mysql Server 5.7\include;

Linker (Linker) general, locate the library directory where you added MySQL in the Add Library directory (Additional library directories). In this way the code can write the #pragma comment (lib, "Libmysql.lib")

You also need to copy the Libmysql.dll to the directory where your executable file is located.

3, a simple demo connection operation database:

1#include <Windows.h>2#include <mysql.h>3#include <string>4#include <iostream>5 6 using namespacestd;7 #pragmaComment (lib, "Libmysql.lib")8 intMain ()9 {Ten  One     Const CharUser[] ="Root"; A     Const CharPswd[] ="Root"; -     Const CharHost[] ="localhost"; -     Const CharDatabase[] =" World"; theUnsignedintPort =3306; - MYSQL Mycont; -Mysql_res *result; - Mysql_row Sql_row; +     intRes; -Mysql_init (&Mycont); +     if(Mysql_real_connect (&mycont, host, user, PSWD, database, port, NULL,0)) A     { atmysql_query (&mycont,"SET NAMES GBK");//set the encoding format -res = mysql_query (&mycont,"SELECT * from City ORDER by ID desc limit");//Positive order ASC, Reverse DESC query, the string must not have a semicolon -         if(!Res) -         { -result = Mysql_store_result (&Mycont); -             if(Result) in             { -                 intIlength =mysql_num_fields (result); to                  while(Sql_row = mysql_fetch_row (Result))//get the specific data +                 { -                      for(intnum =0; Num < ilength; ++num) the                     { *cout << Sql_row[num] <<"\ t"; $                     }Panax Notoginsengcout <<Endl; -                 } the             } +         } A         Else the         { +cout <<"Query SQL failed!"<<Endl; -         } $     } $     Else -     { -cout <<"Connect failed!"<<Endl; the     } -     if(Result! =NULL)Wuyi mysql_free_result (result); theMysql_close (&Mycont); -System"Pause"); Wu     return 0; -  About}

Code Source Network.

Execution Result:

4, Small tip:

A, if you are a 32-bit MySQL library files, compile 32-bit programs, 64-bit MySQL library files compiled 64-bit program OH;

b, I also slag, but I have a dead knock and can calm down the heart, borrowing information on the internet can also be found, rare.

C + + operation MySQL Introduction detailed

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.