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

标签 c++ mysql database raspbian raspberry-pi2

我想借助 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/

相关文章:

c++ - 我不明白为什么这个值在一个函数中计算而不是另一个

类似于 XSTREAM 的 C++ 序列化清理 XML

mysql - 用于提供大量数据的查询的最佳 MySQL 设置?

database - 具有链式调用的 Grails 服务类的事务行为

php - MySQL命名约定,字段名应该包括表名吗?

database - 如何在不加载所有数据库的情况下在列表中添加产品?

C++将dll文件从根目录移动到子文件夹

c++ - 访问内存映射 I/O

mysql - 执行一批 SQL 行的最快方法

mysql - 使用vba创建mysql数据库