Using MySQL in C + +

Source: Internet
Author: User
Tags server port

The software on my computer is as follows:

VS2012

MySQL 5.6

Operating system is 64-bit win8.1


1. Prerequisites

Download and install MySQL

2. Configuration

(1) Add additional Include directories in the project properties of C + + general: My_sql Directory/include

(2) Add additional library directories in the project Properties Connector general: My_sql Directory/lib

(3) Add additional dependencies in the project's Properties connector input : Libmysql.lib

3. Precautions

If you have configured as described above but still do not pass the compilation, prompt:

1> is generating code ...
1>basedb.obj:error LNK2019: unresolved external symbol [email protected], the symbol in the function "Public:bool __thiscall basedb::openconnect (void)" ( Referenced in [email protected]@ @QAE_NXZ]
1>basedb.obj:error LNK2019: unresolved external symbol [email protected], the symbol in the function "Public:bool __thiscall basedb::openconnect (void)" ( Referenced in [email protected]@ @QAE_NXZ]
1>basedb.obj:error LNK2019: unresolved external symbol [email protected], the symbol in function "public:struct st_mysql_res * __thiscall basedb::d oqu Ery (class std::basic_string<char,struct Std::char_traits<char>,class std::allocator<char> > const &) "([email protected]@@[email protected]@[email protected][email protected]@[email protected]@[email protected]@ Referenced in [email protected]@[email protected]@@z]
1>basedb.obj:error LNK2019: unresolved external symbol [email protected], the symbol in function "public:struct st_mysql_res * __thiscall basedb::d oqu Ery (class std::basic_string<char,struct Std::char_traits<char>,class std::allocator<char> > const &) "([email protected]@@[email protected]@[email protected][email protected]@[email protected]@[email protected]@ Referenced in [email protected]@[email protected]@@z]
1>basedb.obj:error LNK2019: unresolved external symbol [email protected], the symbol in function "Public:void __thiscall basedb::closeconnect (void)" ([email protected]@ @QAEXXZ) is quoted
1>dbutil.obj:error LNK2019: unresolved external symbol [email protected], the symbol in the function "public:static bool __cdecl Dbutil::checkuserlogin (c Lass std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & , class std::basic_string<char,struct Std::char_traits<char>,class std::allocator<char> > const &) "([email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@ [email protected]@[email protected]) referenced in
1>e:\flighting\mysqltest\consoleapplication1\debug\consoleapplication1.exe:fatal error Lnk1120:6 an unresolved external command
1>
1> build failed.


The cause of this problem is that you installed the MySQL64 bit, and VS2012 default platform is 32 bit, so it does not support

There are two solutions: (1) project properties in the upper right corner, there is a configuration Manager to change the running platform of your project to X64 (if not, create a new one)

(2) If the project has been introduced to other 32-bit additional libraries, hastily modified to 64 will cause the original library compilation does not pass, so there is no way to surf the internet to search a mysql32 lib and DLL


Finally, if the runtime prompts you not to find Libmysql.dll, copy the Libmysql,dll to System32/syswow64.


code example:

#include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <         Mysql.h> #include <iostream>using namespace Std;int main () {const char user[] = "root";         Username const char pswd[] = "root";    Password const char host[] = "localhost";        or "127.0.0.1" const char table[] = "Test";           database unsigned int port = 3306;    Server Port MYSQL Mycont;    Mysql_res *result;    Mysql_row Sql_row;    Mysql_field *FD;    Char column[32][32];    int res;    Mysql_init (&mycont); if (Mysql_real_connect (&mycont,host,user,pswd,table,port,null,0)) {cout<< "Connect succeed!"        <<endl; mysql_query (&mycont, "SET NAMES GBK");            Set the encoding format, otherwise the Chinese res=mysql_query (&mycont, "SELECT * from Samples") cannot be displayed under CMD;//query if (!res) {              Result=mysql_store_result (&mycont);//Save the queried data to the result if (result) {  int i,j;                cout<< "Number of Result:" << (unsigned long) mysql_num_rows (result) <<endl;                for (I=0;fd=mysql_fetch_field (result); i++)//Gets the column name {strcpy (column[i],fd->name);                } j=mysql_num_fields (Result);                for (i=0;i<j;i++) {printf ("%s\t", Column[i]);                } printf ("\ n");                    while (Sql_row=mysql_fetch_row (result))//Get specific data {for (i=0;i<j;i++)                    {printf ("%s\n", Sql_row[i]);                } printf ("\ n"); }}} else {cout<< "query SQL failed!"        <<endl; }} else {cout<< "Connect failed!"    <<endl; if (result!=null) mysql_free_result (result);//Release result resource Mysql_close (&mycont);//disconnect return 0;}


Using MySQL in C + +

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.