mysql-connector-cpp,mysql-connector-c++ 连接mysql (linux)

centos6.5  ,官网下载包:mysql-connector-c++-1.1.9-linux-glibc2.12-x86-64bit.tar.gz

一、

解压包,将里边的include,lib目录里边的文件和文件夹分别拷到/usr/include ,/usr/lib

二、数据库安装好,配置好用户

三、根据官方提供的案例测试

//test.cpp,内容 :

/* Standard C++ includes */

#include

#include

/*

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

#include

#include

#include

#include

using namespace std;

int main(void)

{

cout << endl;

cout << "Let's have MySQL count from 10 to 1..." << endl;

try {

sql::Driver *driver;

sql::Connection *con;

sql::Statement *stmt;

sql::ResultSet *res;

sql::PreparedStatement *pstmt;

/* Create a connection */

driver = get_driver_instance();

con = driver->connect("tcp://192.168.56.121:3306", "root", "123456");

/* Connect to the MySQL test database */

con->setSchema("test");

stmt = con->createStatement();

stmt->execute("DROP TABLE IF EXISTS test");

stmt->execute("CREATE TABLE test(id INT)");

delete stmt;

/* '?' is the supported placeholder syntax */

pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");

for (int i = 1; i <= 10; i++) {

pstmt->setInt(1, i);

pstmt->executeUpdate();

}

delete pstmt;

/* Select in ascending order */

pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");

res = pstmt->executeQuery();

/* Fetch in reverse = descending order! */

res->afterLast();

while (res->previous())

cout << "\t... MySQL counts: " << res->getInt("id") << endl;

delete res;

delete pstmt;

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;

}

四、编译生成

g++ -c test.cpp -lmysqlcppconn

g++ test.o -lmysqlcppconn

五、运行结果

[root@testicehost test]# ./test

Let's have MySQL count from 10 to 1...

... MySQL counts: 10

... MySQL counts: 9

... MySQL counts: 8

... MySQL counts: 7

... MySQL counts: 6

... MySQL counts: 5

... MySQL counts: 4

... MySQL counts: 3

... MySQL counts: 2

... MySQL counts: 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt 可以通过使用 MySQL Connector/C++连接和操作 MySQL 数据库。MySQL Connector/C++MySQL 官方提供的 C++ 驱动程序,可以在 Qt 项目中使用。 要在 Qt 项目中使用 MySQL Connector/C++,首先需要下载并安装 MySQL Connector/C++。然后,在 Qt 项目中添加 MySQL Connector/C++ 的头文件和库文件。 以下是一些步骤来使用 MySQL Connector/C++: 1. 下载和安装 MySQL Connector/C++。你可以从 MySQL 官方网站下载适用于你的操作系统的安装程序。 2. 在 Qt 项目中添加 MySQL Connector/C++ 的头文件路径。在.pro 文件中添加以下行: ``` INCLUDEPATH += /path/to/mysql-connector-c++/include ``` 3. 在 Qt 项目中添加 MySQL Connector/C++ 的库文件路径。在.pro 文件中添加以下行: ``` LIBS += -L/path/to/mysql-connector-c++/lib -lmysqlcppconn ``` 4. 在代码中包含 MySQL Connector/C++ 的头文件,并使用其提供的类和方法进行数据库连接和操作。例如: ```cpp #include <mysql_driver.h> #include <mysql_connection.h> // 创建连接 sql::mysql::MySQL_Driver *driver; sql::Connection *con; driver = sql::mysql::get_mysql_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "username", "password"); // 执行查询 sql::Statement *stmt; sql::ResultSet *res; stmt = con->createStatement(); res = stmt->executeQuery("SELECT * FROM table"); // 处理结果 while (res->next()) { // 从结果集中获取数据 int id = res->getInt("id"); QString name = QString::fromStdString(res->getString("name")); // 处理数据 // ... } // 清理资源 delete res; delete stmt; delete con; ``` 这样,你就可以在 Qt 项目中使用 MySQL Connector/C++连接和操作 MySQL 数据库了。记得替换代码中的用户名、密码、表名等信息为你实际的数据库信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值