当前位置: 首页 > 面试题库 >

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

严知
2023-03-14
问题内容

我正在尝试从我的网站连接数据库,并使用C
++显示一些行。因此,基本上我正在尝试制作一个从站点数据库中的表中进行选择查询的应用程序。现在,这必须可行,因为我已经看到大量的应用程序正在这样做。

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


问题答案:

在这里找到:

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


 类似资料:
  • 问题内容: 如何使用java连接到mysql数据库? 问题答案: 逐步说明如何安装MySQL和JDBC以及如何使用它: 1.下载并安装MySQL服务器。只需按照通常的方式进行即可。每次更改时都请记住端口号。默认情况下3306。 2.下载 JDBC驱动程序并放入classpath,解压缩ZIP文件并将包含的JAR文件放入classpath。特定于供应商的JDBC驱动程序是 JDBC API的具体实现

  • 问题内容: 如何使用python程序连接到MySQL数据库? 问题答案: 分三步使用Python 2连接到MYSQL 1-设定 在执行任何操作之前,必须安装MySQL驱动程序。与PHP不同,默认情况下,Python仅安装SQLite驱动程序。最常用的软件包是MySQLdb,但很难使用进行安装。请注意,MySQLdb仅支持Python 2。 对于Windows用户,你可以获取MySQLdb的exe。

  • 我正在尝试使用SpringBoot连接MySQL数据库,但我遇到以下错误: 应用程序无法以类路径启动:[文件:/C:/Dev/Repositorios/jira quality/target/classes/,文件:/C:/Users/jboscod/.m2/repository/mysql/mysql连接器java/8.0.21/mysql-connector-java-8.0.21.jar,文

  • 所以我正在尝试使用突出显示的连接进行连接。我使用密码'abcdefghijkl'登录到SQL环境。我正在尝试连接到名为“flight_school”的数据库 我的python脚本看起来就是这样。` 导入mysql.connector mydb=mysql.connector.connect(“localhost”,“root”,“abcdefghijkl”,“flight_school”“)打印(

  • 问题内容: 我正在尝试从iPhone连接到远程mysql数据库。我搜索了许多网站,但没有找到任何帮助。如果有人为此工作,请发送解决方案。 问题答案: 假设您具有服务器端编程(例如PHP或Rails)的经验,则可以仅从URL内容创建NSArray,在其中建立与MySQL服务器的连接并以ASCII或XML格式打印所需的结果。 对帮助格式化服务器页面打印的结果也很有用:http : //develope

  • 问题内容: 这个问题已经在这里有了答案 : 将查询错误转换为MySQLi中的异常 (3个答案) 9个月前关闭。 我碰到了这个: PHP错误处理:die()VS trigger_error()VS throw异常 并且理解抛出异常更好 我如何在此代码中替换die并使用throw异常:- 问题答案: