MySQL 连接器 C++ - 犯错误1

发布于 2024-08-05 12:56:46 字数 1460 浏览 2 评论 0原文

我正在用 C++ 编写一个应用程序(使用带有 Linux GCC 的 Eclipse),该应用程序应该与我的 MySQL 服务器交互。 我已经下载了 MySQL Connector C++ a,进行了预编译,并将文件复制到目录(/usr/lib、/usr/include)中。我在 Eclipse 中项目属性的 GCC C++ 链接器部分中引用了(“mysqlcppconn”)。我的代码直接来自MySQL参考(Hello World),除了我删除了错误处理和最后的删除语句(我什至没有到达那里,所以有什么意义)

#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;

int main(void)
{
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
  con->setSchema("test");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");

  while (res->next()) 
  {
    cout << "\t... MySQL replies: ";
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    cout << res->getString(1) << endl;
  }  
return EXIT_SUCCESS;
}

现在当我编译这个时,它给了我一个 make error 1 ,手册页说这意味着我需要重新编译我的包。我已经尝试过,它不起作用。

我似乎到处都陷入了死胡同。谁能告诉我如何解决这个问题? (make 真的可以告诉我应该重新编译什么。)有人对我出错的地方有任何提示/建议/想法吗?

编辑 - 对代码进行了一些更改,但总体思路是相同的。

更新 - 如果我使用终端而不是 exlipse 来 make,它告诉我问题显然是连接器需要 libstdc++.so.5,而我有 libstdc++.so.6。

I'm writing an application in C++ (using Eclipse with Linux GCC) that's supposed to interact with my MySQL server.
I've downloaded the MySQL Connector C++ a, precompiled, and copied the files into the directories (/usr/lib, /usr/include). I've referenced in in the GCC C++ Linker Section of the Project Properties in Eclipse ( "mysqlcppconn"). My code comes directly from the MySQL Reference (Hello World) except I removed error handling and the delete statements at the end (I'm not even getting there so whats the point)

#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;

int main(void)
{
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
  con->setSchema("test");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");

  while (res->next()) 
  {
    cout << "\t... MySQL replies: ";
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    cout << res->getString(1) << endl;
  }  
return EXIT_SUCCESS;
}

now when I compile this, It gives me a make error 1, which the man page says means I need to recompile my packages. I've tried it, it doesn't work.

I seem to have hit dead ends everywhere. Can anyone tell me how to fix this problem? (make could really tell me what I should recompile.) Anyone have any tips/suggestions/ideas where I went wrong?

Edit - Chagned the code a bit, but the general idea is the same.

Update - If I use the terminal instead of exlipse to make, it tells me that the problem is obviously that the connector wants libstdc++.so.5 whilst I have libstdc++.so.6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-08-12 12:56:46

解决方案非常简单 - 编译您自己的连接器。我用 1.0.5 版本的连接器做到了。为此,您需要通过

sudo apt-get install mysql-client

安装包在您下载(并解压)的源包目录中,输入

cmake 。

显然,在驱动程序的三个文件中,引用了 snprintf 和 printf,但不包含 stdio.h 标头。我将

#include

添加到每个文件,然后在终端中输入

make

然后,我将文件复制到我的 lib 目录

sudo cp path/to/driver/libmysqlcppconn* /usr/lib/

一切都很有魅力。

The Solution is quite simple - compile your own Connector. I did it with the 1.0.5 version of the Connector. to do it, you need to install the package via

sudo apt-get install mysql-client

In the directory of the source package you downloaded (and extracted), type

cmake .

apparently, in three files of the driver, references are made to snprintf and printf, without including the stdio.h header. I added

#include <stdio.h>

to each file and then, in the terminal, typed

make

then, I copied the Files to my lib directory

sudo cp path/to/driver/libmysqlcppconn* /usr/lib/

and everythign worked a charm.

夏末染殇 2024-08-12 12:56:46

下载并安装适合您的发行版的 C++ MySQL 开发包。对于 ubuntu,运行 # sudo apt-get install libmysql++-dev。

Download and install the C++ MySQL development packages for your distribution. For ubuntu, run # sudo apt-get install libmysql++-dev.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文