gpt4 book ai didi

c++ - 将 Raspberry Pi 2 连接到外部远程数据库

转载 作者:搜寻专家 更新时间:2023-10-30 20:52:20 24 4
gpt4 key购买 nike

我想借助 C++ 将我的 Raspberry Pi 2 连接到 000webhost.com 上的外部 MySQL 数据库。

#include <stdlib.h>
#include <iostream>
#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)
{
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;

/* Create a connection */
driver = get_driver_instance();
con = driver->connect("<000webhost mysql address>", "<username>", "<password>");
/* Connect to the MySQL test database */
con->setSchema("<database>");

stmt = con->createStatement();
res = stmt->executeQuery("<sql statement>"); // 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;
}

我收到一条错误消息,指出 mysql_connection.h 文件丢失。我不知道我做错了什么,或者是否有更简单或更简单的方法。请帮我解决这个问题。谢谢。

最佳答案

I got an error saying that the mysql_connection.h file was missing.

按照您描述的错误消息和对我的问题“您是否安装了 mysql_connection.h 的源代码?”的回答是:

I didn't install mysql on the raspberry pi since I was trying to access an external database.

(这不是我问的。)#include "mysql_connection.h 不起作用的原因似乎是因为库不存在。

Connector/C不需要安装 MySQL,但Connector/C必须> 安装以包含库中的 header ,according to the documentation .

MySQL 的Connector/C需要 boost ;然而 Raspian 已经安装了 boost,尽管这可能会带来它自己的一系列问题,这些问题已得到讨论和解决 here如果您在使用 boost 时遇到问题。 (如果你的 Raspberry Pi 有 NOOBS,我建议切换到 Raspian。)

虽然链接在我的评论中,但安装 Connector/C 库的说明可从 MySQL's documentation 获得。 .

我收集了一些资源和链接,它们应该可以提供一些额外的帮助:

  1. How to Install Third Party Libraries
  2. How to #include third party libraries
  3. Installing Connector/C++ from Source on Unix and Unix-Like Systems

关于c++ - 将 Raspberry Pi 2 连接到外部远程数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37668761/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com