如何使用c++连接mySQL数据库?

时间:2022-07-20 03:01:35

I'm trying to connect the database from my website and display some rows using C++. So bascily I'm trying to make an application that does a select query from a table from my site database. Now, this must be possible because I've seen tons of applications doing it.

我尝试从我的网站连接数据库,并使用c++显示一些行。因此,bascily我正在尝试创建一个应用程序,从我的站点数据库中从表中执行select查询。现在,这必须是可能的,因为我已经看到了大量的应用程序。

How do I do this? Can some one make an example and tell me what libraries I should be using?

我该怎么做呢?有人能举个例子告诉我应该使用哪些库吗?

4 个解决方案

#1


25  

Clearly, you've not been looking in the right corners of the Internet, because I found an example in a few seconds:

很明显,你没有在互联网的正确角落里寻找,因为我在几秒钟内找到了一个例子:

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
   AS _message'..." << endl;

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
  /* Connect to the MySQL test database */
  con->setSchema("test");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace with your statement
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column fata by numeric offset, 1 is the first column */
    cout << res->getString(1) << endl;
  }
  delete res;
  delete stmt;
  delete con;

} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " »
     << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

#2


7  

Finally I could successfully compile a program with C++ connector in Ubuntu 12.04 I have installed the connector using this command

最后,我可以在Ubuntu 12.04中使用c++连接器成功地编译一个程序,我已经使用这个命令安装了连接器。

'apt-get install libmysqlcppconn-dev'

Initially I faced the same problem with "undefined reference to `get_driver_instance' " to solve this I declare my driver instance variable of MySQL_Driver type. For ready reference this type is defined in mysql_driver.h file. Here is the code snippet I used in my program.

最初,我遇到了同样的问题,“没有定义的‘get_driver_instance’”来解决这个问题,我声明了我的驱动程序实例变量MySQL_Driver类型。为便于准备,该类型在mysql_driver中定义。h文件。这是我在程序中使用的代码片段。

sql::mysql::MySQL_Driver *driver;
try {     
    driver = sql::mysql::get_driver_instance();
}

and I compiled the program with -l mysqlcppconn linker option

我用-l mysqlcppconn链接器编译了这个程序。

and don't forget to include this header

别忘了加上这个标题。

#include "mysql_driver.h" 

#3


5  

Yes, you will need the mysql c++ connector library. Read on below, where I explain how to get the example given by mysql developers to work.

是的,您将需要mysql c++连接器库。下面,我将介绍如何获得mysql开发人员提供的工作示例。

Note(and solution): IDE: I tried using Visual Studio 2010, but just a few sconds ago got this all to work, it seems like I missed it in the manual, but it suggests to use Visual Studio 2008. I downloaded and installed VS2008 Express for c++, followed the steps in chapter 5 of manual and errors are gone! It works. I'm happy, problem solved. Except for the one on how to get it to work on newer versions of visual studio. You should try the mysql for visual studio addon which maybe will get vs2010 or higher to connect successfully. It can be downloaded from mysql website

注意(和解决方案):IDE:我尝试使用Visual Studio 2010,但是仅仅几个sconds之前就把这些都用到工作中了,看起来我在手册中漏掉了,但是它建议使用Visual Studio 2008。我下载并安装了VS2008 Express的c++,按照手册第五章的步骤,错误消失了!它的工作原理。问题解决了,我很高兴。除了关于如何让它在新版本的visual studio上工作。您应该尝试mysql visual studio addon,它可能会获得vs2010或更高的连接成功。可以从mysql网站下载。

Whilst trying to get the example mentioned above to work, I find myself here from difficulties due to changes to the mysql dev website. I apologise for writing this as an answer, since I can't comment yet, and will edit this as I discover what to do and find the solution, so that future developers can be helped.(Since this has gotten so big it wouldn't have fitted as a comment anyways, haha)

在试图获得上面提到的工作的例子时,我发现自己在这里遇到了一些困难,因为mysql开发网站的变化。我为此而道歉,因为我现在还不能评论,并且会在我发现该做什么和找到解决方案的时候编辑它,以便将来的开发人员能够得到帮助。(因为它太大了,所以它不适合做任何评论,哈哈)

@hd1 link to "an example" no longer works. Following the link, one will end up at the page which gives you link to the main manual. The main manual is a good reference, but seems to be quite old and outdated, and difficult for new developers, since we have no experience especially if we missing a certain file, and then what to add.

@hd1链接到“一个示例”不再有效。在链接之后,将会出现在页面上,链接到主手册。主要的手册是一个很好的参考,但是对于新开发人员来说似乎很老旧,而且很困难,因为我们没有经验,特别是如果我们丢失了某个文件,然后添加什么。

@hd1's link has moved, and can be found with a quick search by removing the url components, keeping just the article name, here it is anyways: http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

@hd1的链接已经移动了,可以通过删除url组件来快速搜索,只保留文章的名称,这里是任何方法:http://dev.mysql.com/doc/connector- cpp/en/cpp-exampl-completionexample -1.html。

Getting 7.5 MySQL Connector/C++ Complete Example 1 to work

获得7.5 MySQL连接器/ c++完整示例1工作。

Downloads:

下载:

-Get the mysql c++ connector, even though it is bigger choose the installer package, not the zip.

-获取mysql c++连接器,即使它是更大的选择安装程序包,而不是zip。

-Get the boost libraries from boost.org, since boost is used in connection.h and mysql_connection.h from the mysql c++ connector

-从boost.org获取boost库,因为boost用于连接。h和mysql_connection。来自mysql c++连接器。

Now proceed:

现在继续进行:

-Install the connector to your c drive, then go to your mysql server install folder/lib and copy all libmysql files, and paste in your connector install folder/lib/opt

-安装连接器到你的c驱动器,然后到你的mysql服务器安装文件夹/lib,复制所有的libmysql文件,并粘贴到你的连接器安装文件夹/lib/opt。

-Extract the boost library to your c drive

-将boost库提取到c驱动器。

Next:

下一个:

It is alright to copy the code as it is from the example(linked above, and ofcourse into a new c++ project). You will notice errors:

可以将代码复制到示例中(如上所述,并将其转换为一个新的c++项目)。你会注意到错误:

-First: change

首次提出:改变

cout << "(" << __FUNCTION__ << ") on line " »
 << __LINE__ << endl;

to

cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;

Not sure what that tiny double arrow is for, but I don't think it is part of c++

不知道这个小的双箭头代表什么,但我不认为它是c++的一部分。

-Second: Fix other errors of them by reading Chapter 5 of the sql manual, note my paragraph regarding chapter 5 below

-第二:通过阅读sql手册的第5章修正他们的其他错误,注意下面的第5章。

[Note 1]: Chapter 5 Building MySQL Connector/C++ Windows Applications with Microsoft Visual Studio If you follow this chapter, using latest c++ connecter, you will likely see that what is in your connector folder and what is shown in the images are quite different. Whether you look in the mysql server installation include and lib folders or in the mysql c++ connector folders' include and lib folders, it will not match perfectly unless they update the manual, or you had a magic download, but for me they don't match with a connector download initiated March 2014.

如果你使用最新的c++连接程序,你很可能会看到你的连接器文件夹里的内容和图片中显示的内容是完全不同的。你看起来在mysql服务器是否安装包括和lib文件夹或c++ mysql连接器文件夹的包括和lib文件夹,它不会搭配得很好,除非他们更新手册,或者你有一个神奇的下载,但对我来说他们不匹配连接器下载2014年3月启动。

Just follow that chapter 5,

跟着第五章,

-But for c/c++, General, Additional Include Directories include the "include" folder from the connector you installed, not server install folder

但是对于c/c++,一般来说,额外的包含目录包括你安装的连接器的“Include”文件夹,而不是服务器安装文件夹。

-While doing the above, also include your boost folder see note 2 below

-在做上述工作的同时,也包括你的boost文件夹,参见下面的注释2。

-And for the Linker, General.. etc use the opt folder from connector/lib/opt

-对于林克,将军…etc使用连接器/lib/opt的opt文件夹。

*[Note 2]*A second include needs to happen, you need to include from the boost library variant.hpp, this is done the same as above, add the main folder you extracted from the boost zip download, not boost or lib or the subfolder "variant" found in boostmainfolder/boost.. Just the main folder as the second include

*[注释2]*第二个包含需要发生,您需要从boost库变体中包含。hpp,这和上面一样,添加从boost zip下载中提取的主文件夹,而不是boost或lib或在boostmainfolder/boost中找到的子文件夹“变体”。只有主文件夹作为第二个包含。

Next:

下一个:

What is next I think is the Static Build, well it is what I did anyways. Follow it.

接下来我认为是静态构建,这是我做过的。跟随它。

Then build/compile. LNK errors show up(Edit: Gone after changing ide to visual studio 2008). I think it is because I should build connector myself(if you do this in visual studio 2010 then link errors should disappear), but been working on trying to get this to work since Thursday, will see if I have the motivation to see this through after a good night sleep(and did and now finished :) ).

然后构建/编译。LNK错误出现(编辑:将ide更改为visual studio 2008)。我认为这是因为我应该建立连接器(如果你这样做在visual studio 2010中然后链接错误应该消失),但一直在试图得到这个星期四以后工作,会看到如果我有动力通过一个好觉后看到这个(和现在完成:))。

#4


2  

I had to include -lmysqlcppconn to my build in order to get it to work.

我必须将-lmysqlcppconn添加到我的构建中,以便让它工作。

#1


25  

Clearly, you've not been looking in the right corners of the Internet, because I found an example in a few seconds:

很明显,你没有在互联网的正确角落里寻找,因为我在几秒钟内找到了一个例子:

/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' »
   AS _message'..." << endl;

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
  /* Connect to the MySQL test database */
  con->setSchema("test");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace with your statement
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column fata by numeric offset, 1 is the first column */
    cout << res->getString(1) << endl;
  }
  delete res;
  delete stmt;
  delete con;

} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " »
     << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

#2


7  

Finally I could successfully compile a program with C++ connector in Ubuntu 12.04 I have installed the connector using this command

最后,我可以在Ubuntu 12.04中使用c++连接器成功地编译一个程序,我已经使用这个命令安装了连接器。

'apt-get install libmysqlcppconn-dev'

Initially I faced the same problem with "undefined reference to `get_driver_instance' " to solve this I declare my driver instance variable of MySQL_Driver type. For ready reference this type is defined in mysql_driver.h file. Here is the code snippet I used in my program.

最初,我遇到了同样的问题,“没有定义的‘get_driver_instance’”来解决这个问题,我声明了我的驱动程序实例变量MySQL_Driver类型。为便于准备,该类型在mysql_driver中定义。h文件。这是我在程序中使用的代码片段。

sql::mysql::MySQL_Driver *driver;
try {     
    driver = sql::mysql::get_driver_instance();
}

and I compiled the program with -l mysqlcppconn linker option

我用-l mysqlcppconn链接器编译了这个程序。

and don't forget to include this header

别忘了加上这个标题。

#include "mysql_driver.h" 

#3


5  

Yes, you will need the mysql c++ connector library. Read on below, where I explain how to get the example given by mysql developers to work.

是的,您将需要mysql c++连接器库。下面,我将介绍如何获得mysql开发人员提供的工作示例。

Note(and solution): IDE: I tried using Visual Studio 2010, but just a few sconds ago got this all to work, it seems like I missed it in the manual, but it suggests to use Visual Studio 2008. I downloaded and installed VS2008 Express for c++, followed the steps in chapter 5 of manual and errors are gone! It works. I'm happy, problem solved. Except for the one on how to get it to work on newer versions of visual studio. You should try the mysql for visual studio addon which maybe will get vs2010 or higher to connect successfully. It can be downloaded from mysql website

注意(和解决方案):IDE:我尝试使用Visual Studio 2010,但是仅仅几个sconds之前就把这些都用到工作中了,看起来我在手册中漏掉了,但是它建议使用Visual Studio 2008。我下载并安装了VS2008 Express的c++,按照手册第五章的步骤,错误消失了!它的工作原理。问题解决了,我很高兴。除了关于如何让它在新版本的visual studio上工作。您应该尝试mysql visual studio addon,它可能会获得vs2010或更高的连接成功。可以从mysql网站下载。

Whilst trying to get the example mentioned above to work, I find myself here from difficulties due to changes to the mysql dev website. I apologise for writing this as an answer, since I can't comment yet, and will edit this as I discover what to do and find the solution, so that future developers can be helped.(Since this has gotten so big it wouldn't have fitted as a comment anyways, haha)

在试图获得上面提到的工作的例子时,我发现自己在这里遇到了一些困难,因为mysql开发网站的变化。我为此而道歉,因为我现在还不能评论,并且会在我发现该做什么和找到解决方案的时候编辑它,以便将来的开发人员能够得到帮助。(因为它太大了,所以它不适合做任何评论,哈哈)

@hd1 link to "an example" no longer works. Following the link, one will end up at the page which gives you link to the main manual. The main manual is a good reference, but seems to be quite old and outdated, and difficult for new developers, since we have no experience especially if we missing a certain file, and then what to add.

@hd1链接到“一个示例”不再有效。在链接之后,将会出现在页面上,链接到主手册。主要的手册是一个很好的参考,但是对于新开发人员来说似乎很老旧,而且很困难,因为我们没有经验,特别是如果我们丢失了某个文件,然后添加什么。

@hd1's link has moved, and can be found with a quick search by removing the url components, keeping just the article name, here it is anyways: http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-complete-example-1.html

@hd1的链接已经移动了,可以通过删除url组件来快速搜索,只保留文章的名称,这里是任何方法:http://dev.mysql.com/doc/connector- cpp/en/cpp-exampl-completionexample -1.html。

Getting 7.5 MySQL Connector/C++ Complete Example 1 to work

获得7.5 MySQL连接器/ c++完整示例1工作。

Downloads:

下载:

-Get the mysql c++ connector, even though it is bigger choose the installer package, not the zip.

-获取mysql c++连接器,即使它是更大的选择安装程序包,而不是zip。

-Get the boost libraries from boost.org, since boost is used in connection.h and mysql_connection.h from the mysql c++ connector

-从boost.org获取boost库,因为boost用于连接。h和mysql_connection。来自mysql c++连接器。

Now proceed:

现在继续进行:

-Install the connector to your c drive, then go to your mysql server install folder/lib and copy all libmysql files, and paste in your connector install folder/lib/opt

-安装连接器到你的c驱动器,然后到你的mysql服务器安装文件夹/lib,复制所有的libmysql文件,并粘贴到你的连接器安装文件夹/lib/opt。

-Extract the boost library to your c drive

-将boost库提取到c驱动器。

Next:

下一个:

It is alright to copy the code as it is from the example(linked above, and ofcourse into a new c++ project). You will notice errors:

可以将代码复制到示例中(如上所述,并将其转换为一个新的c++项目)。你会注意到错误:

-First: change

首次提出:改变

cout << "(" << __FUNCTION__ << ") on line " »
 << __LINE__ << endl;

to

cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;

Not sure what that tiny double arrow is for, but I don't think it is part of c++

不知道这个小的双箭头代表什么,但我不认为它是c++的一部分。

-Second: Fix other errors of them by reading Chapter 5 of the sql manual, note my paragraph regarding chapter 5 below

-第二:通过阅读sql手册的第5章修正他们的其他错误,注意下面的第5章。

[Note 1]: Chapter 5 Building MySQL Connector/C++ Windows Applications with Microsoft Visual Studio If you follow this chapter, using latest c++ connecter, you will likely see that what is in your connector folder and what is shown in the images are quite different. Whether you look in the mysql server installation include and lib folders or in the mysql c++ connector folders' include and lib folders, it will not match perfectly unless they update the manual, or you had a magic download, but for me they don't match with a connector download initiated March 2014.

如果你使用最新的c++连接程序,你很可能会看到你的连接器文件夹里的内容和图片中显示的内容是完全不同的。你看起来在mysql服务器是否安装包括和lib文件夹或c++ mysql连接器文件夹的包括和lib文件夹,它不会搭配得很好,除非他们更新手册,或者你有一个神奇的下载,但对我来说他们不匹配连接器下载2014年3月启动。

Just follow that chapter 5,

跟着第五章,

-But for c/c++, General, Additional Include Directories include the "include" folder from the connector you installed, not server install folder

但是对于c/c++,一般来说,额外的包含目录包括你安装的连接器的“Include”文件夹,而不是服务器安装文件夹。

-While doing the above, also include your boost folder see note 2 below

-在做上述工作的同时,也包括你的boost文件夹,参见下面的注释2。

-And for the Linker, General.. etc use the opt folder from connector/lib/opt

-对于林克,将军…etc使用连接器/lib/opt的opt文件夹。

*[Note 2]*A second include needs to happen, you need to include from the boost library variant.hpp, this is done the same as above, add the main folder you extracted from the boost zip download, not boost or lib or the subfolder "variant" found in boostmainfolder/boost.. Just the main folder as the second include

*[注释2]*第二个包含需要发生,您需要从boost库变体中包含。hpp,这和上面一样,添加从boost zip下载中提取的主文件夹,而不是boost或lib或在boostmainfolder/boost中找到的子文件夹“变体”。只有主文件夹作为第二个包含。

Next:

下一个:

What is next I think is the Static Build, well it is what I did anyways. Follow it.

接下来我认为是静态构建,这是我做过的。跟随它。

Then build/compile. LNK errors show up(Edit: Gone after changing ide to visual studio 2008). I think it is because I should build connector myself(if you do this in visual studio 2010 then link errors should disappear), but been working on trying to get this to work since Thursday, will see if I have the motivation to see this through after a good night sleep(and did and now finished :) ).

然后构建/编译。LNK错误出现(编辑:将ide更改为visual studio 2008)。我认为这是因为我应该建立连接器(如果你这样做在visual studio 2010中然后链接错误应该消失),但一直在试图得到这个星期四以后工作,会看到如果我有动力通过一个好觉后看到这个(和现在完成:))。

#4


2  

I had to include -lmysqlcppconn to my build in order to get it to work.

我必须将-lmysqlcppconn添加到我的构建中,以便让它工作。