我试图连接数据库从我的网站,并显示一些行使用C++.

我该怎么做?有人能举个例子告诉我应该使用哪些库吗?

推荐答案

找到here个:

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

Mysql相关问答推荐

JOOQ-如何在一个查询中引用多个(但不是所有)表中的所有字段

使用由其中一个表的列规定的条件连接两个表

如何在WooCommerce中更新pm_Virtual.meta_Value=#39;否

如何查询一行

在两个日期之间生成每个月的1行数据.

MySQL 关于 JSON 数组和子查询的问题

MYSQL 删除两个已知字符串之间的字符串

MySQL如何在小时和分钟之间 Select 时间

了解 Spring 和数据库级别的事务?

处理 Visits 表中数百万行的最佳方法是什么?

SQL 查找边界之间的值

动态创建内联 SQL 表(用于排除左连接)

不正确的整数(2147483647)被插入到 MySQL 中?

在连接条件上使用 IS NULL 或 IS NOT NULL - 理论问题

mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值

Drupal 的默认密码加密方法是什么?

我应该使用什么列数据类型来存储大量文本或 html

MySQL 导出到 outfile:CSV 转义字符

使用 Docker 我收到错误:SQLSTATE[HY000] [2002] 没有这样的文件或目录

如何使用 XML_LOAD() 将 XML 文件导入 MySQL 数据库表;功能