c web mysql数据库,如何使用C ++连接mySQL数据库

I'm trying to connect the database from my website and display some rows using C++.

So bascily I'm trying to make an application that does a select query from a table from my site database. Now, this must be possible because I've seen tons of applications doing it.

How do I do this? Can some one make an example and tell me what libraries I should be using?

解决方案

Clearly, you've not been looking in the right corners of the Internet, because I found an example in a few seconds:

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

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值