如何在Mac OS上的C ++中连接到MySQL?

业余的:

早上好,我正在尝试使用C ++连接到mysql。我正在使用Macbook Pro。

我使用原始文档中的简单代码

当我尝试编译程序时,它将失败。

main.cpp:15:10: fatal error: 'cppconn/driver.h' file not found
#include <cppconn/driver.h> 

我在这里找到了行不通的解决方案此外,文档中的解决方案也不起作用。

我的代码:

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

我试图通过安装cppconn

brew install mysql-connector-c++

谢谢您的帮助。

业余的:

我找到了完整的解决方案。只需从此站点安装软件包即可

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Windows的Python 3中连接到MySQL?

无法连接到在我的Mac上运行的mysql

在Mac OS X上无法连接到Docker守护程序

在Mac OS上如何停止MySQL?

无法连接到Mac上的MySQL Workbench。无法连接到“ 127.0.0.1”上的MySQL服务器(61)Mac Macintosh

如何从主机连接到在Docker上运行的MySQL

如何在Mac OS上的C ++中模拟打字/按键?

如何从Windows计算机中的Docker容器内部连接到localhost上的MySQL服务器?

如何在Mac上将Laravel连接到MAMP mysql

如何在C#中连接到RavenHQ

如何使用Python3连接到在Mac上的docker中运行的SQL Server?

如何在Mac OS上使用“ nm”在C程序中显示本地(非外部)符号?

如何在设备上预览我的Android应用?(它通过USB连接到我的Mac。)

如何将C#连接到Atom中的远程MySQL Server?

如何在Julia中连接到MySQL数据库

如何在Mac OS(客户端)和服务器(ubuntu)上设置ssh以从丢失的临时连接中恢复

如何在Mac OS X(Snow Leopard)上卸载MySQL?

如何在Mac OS X上创建ssh连接终端快捷方式?

如何在Mac OS X Mavericks上的std C ++库中链接?

如何在Java中的Mac OS X上的Microsoft Word中打开文件?

如何在Mac OS X Yosemite上为MySQL重置我的root密码?

如何在Visual Studio for Mac版本预览上连接到TFS

如何通过WSL终端连接到XAMPP上运行的MySQL

如何在Mac OS上从iPhone连接到本地主机

如何使用 React Native 连接到 Android TV 模拟器(在 Mac OS 上)?

如何在 CentOS 上从 C UDF 连接到 MySQL 数据库

如何在 MAC os 上的 pycharm 中配置 vagrant venv?

你如何在 Docker 中设置 MySQL 并连接到它?

如何通过 Mac 上的客户端连接到 MySQL 数据库?