C++ 如何使用C++连接mySQL数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16424828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:20:45  来源:igfitidea点击:

How to connect mySQL database using C++

c++mysql

提问by lepel100

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++ 显示一些行。所以基本上我正在尝试制作一个应用程序,该应用程序从我的站点数据库中的表中执行选择查询。现在,这一定是可能的,因为我已经看到大量应用程序在这样做。

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

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

采纳答案by hd1

Found here:

这里找到:

/* 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;
}

回答by munawwerali

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" 

回答by Alan Ball

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,但就在几秒钟前,这一切都可以正常工作,似乎我在手册中遗漏了它,但它建议使用 Visual Studio 2008。我下载并安装了VS2008 Express for c++,按照手册第5章的步骤,错误消失了!有用。我很高兴,问题解决了。除了关于如何让它在新版本的 Visual Studio 上工作的那个。您应该尝试使用 mysql for Visual Studio 插件,它可能会获得 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/connector -cpp-examples-complete-example-1.html

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

使 7.5 MySQL Connector/C++ Complete Example 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 用于来自 mysql c++ 连接器的 connection.h 和 mysql_connection.h

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 StudioIf 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.

[注1]:第5章大厦的MySQL连接器/ C ++ Windows应用程序与Microsoft Visual Studio如果按照这一章中,采用最新的C ++连接器,你可能会看到什么是显示在图像有很大的不同是什么在你的连接器文件夹, . 无论您是查看 mysql 服务器安装的 include 和 lib 文件夹,还是 mysql c++ 连接器文件夹的 include 和 lib 文件夹,除非他们更新手册,或者您有一个神奇的下载,否则它不会完美匹配,但对我来说他们没有与 2014 年 3 月启动的连接器下载相匹配。

Just follow that chapter 5,

只要按照第5章,

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

- 但对于 c/c++,General,Additional Include Directories 包括您安装的连接器中的“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

- 对于链接器、General.. 等,请使用 connector/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库variant.hpp中包含,这与上述相同,添加从boost zip下载中提取的主文件夹,而不是boost或lib或在 boostmainfolder/boost 中找到的子文件夹“variant”.. 只是作为第二个包含的主文件夹

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 中执行此操作,则链接错误应该会消失),但自周四以来一直在努力使其正常工作,看看我是否有动力通过经过一夜好眠(做了,现在完成了:))。

回答by Nick Kotenberg

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

我必须在我的构建中包含 -lmysqlcppconn 才能使其正常工作。