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

如何使用C ++连接MySQL数据库,第1张

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

在这里找到

#include <stdlib.h>#include <iostream>#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;    driver = get_driver_instance();  con = driver->connect("tcp://127.0.0.1:3306", "root", "root");    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: ";        cout << res->getString("_message") << endl;    cout << "t... MySQL says it again: ";        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 pre: " << e.getErrorCode();  cout << ", SQLState: " << e.getSQLState() << " )" << endl;}cout << endl;return EXIT_SUCCESS;}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5062658.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存