如何使用C (Ubuntu)连接到MySQL数据库
How to connect to MySQL database with C++ (ubuntu)
我不确定如何使用Ubuntu Linux上的C 连接到MySQL数据库。具体来说,我希望能够创建一个名为" new "的数据库。我不确定要在哪里放置查询。以下是我正在使用的代码。
驱动程序= get_driver_instance(); 给我错误消息 undefined引用'get_driver_instance'
#include <cstdlib>
#include "mysql_driver.h"
#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(int argc, char** argv) {
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();
/* Write your own driver details */
con = driver->connect("tcp://127.0.0.1:3306", "root", "pass");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
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 data 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 ";
cout << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
在您的终端上尝试此命令:
~# sudo g++ -Wall -I/usr/include/cppconn -o <output name> <codename.cpp> -L/usr/lib -lmysqlcppconn
相关文章:
- 无法在C++中建立与MySQL数据库的连接
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- MySQL 连接器/C++.尝试在此过程中停止连接到数据库
- 数据库连接并将数据从C ++程序保存到MySQL
- 为什么mysql拒绝本地连接
- 我不能使用 mysql 数据库连接同时使用插入和选择.C++
- mysql 远程连接 - mysql CL 客户端工作,C++应用程序不工作
- 从C++连接MySQL时程序错误中"error: stray ‘302’"编译错误
- 在Eclipse中找不到用于连接MySQL的C++代码的库
- 加载libmysql.dll连接mySQL数据库
- 连接mysql数据库c++
- 如何使用c++连接mySQL数据库
- 从c++连接MySQL:错误LNK2001:未解析的外部符号
- 使用c++和函数_main中引用的[ERROR]未解析的外部符号_mysql_error@4连接mysql
- Qt Creator _IMP连接MySql时出现错误
- 远程连接MySQL是安全的吗?
- 使用eclipse在c++中连接mysql
- 如何使用c++连接mysql
- 从c++连接mysql最好的框架是什么?
- 使用Poco连接MySQL失败