接下来我会详细讲解如何解决C++与mysql连接遇到的常见问题。
C++与mysql连接遇到的问题汇总
安装mysql驱动
在C++中连接mysql需要用到mysql的驱动,因此要先安装mysql驱动。
Windows平台下的mysql驱动安装
- 下载mysql C++ Connector
mysqldownload.csdn.net/pr/d/1575/download/36
- 解压安装
解压后得到一个lib文件夹和一个include文件夹,将lib文件夹中的libmysql.lib(如果是64位系统的话,是libmysql64.lib)复制到C++的lib文件夹下面,将include文件夹中的内容全部复制到C++的include文件夹下面。
注意:lib文件夹和include文件夹中的文件都要复制到正确的位置,否则连接mysql时会出现找不到文件的错误。
Linux平台下的mysql驱动安装
- Ubuntu系统
在终端中输入以下命令:
sudo apt-get install libmysqlcppconn-dev
等待安装完成后,就可以用C++连接mysql了。
- CentOS系统
在终端中输入以下命令:
sudo yum install mysql-connector-c++-devel
等待安装完成后,就可以用C++连接mysql了。
建立连接
在使用C++连接mysql之前,需要先建立连接。
#include <iostream>
#include "mysql_connection.h"
#include "mysql_driver.h"
#include "cppconn/exception.h"
#include "cppconn/resultset.h"
#include "cppconn/statement.h"
using namespace std;
using namespace sql::mysql;
int main() {
try {
/* 建立连接 */
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
stmt = conn->createStatement();
/* 连接成功后,可以在这里进行操作 */
stmt->execute("CREATE DATABASE IF NOT EXISTS test");
stmt->execute("USE test");
stmt->execute("CREATE TABLE IF NOT EXISTS user ("
"id INT NOT NULL AUTO_INCREMENT,"
"name VARCHAR(20) NOT NULL,"
"age INT NOT NULL,"
"PRIMARY KEY (id))");
cout << "created table user" << endl;
/* 关闭连接 */
delete stmt;
delete conn;
} 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;
}
return 0;
}
- 使用指定的用户名和密码建立连接:
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
这里的127.0.0.1是mysql的IP地址,3306是mysql的端口号,root是用户名,password是密码。如果用户名和密码是正确的,连接将建立成功。
- 连接成功后,可以使用stmt对象进行mysql相关操作。
stmt->execute("CREATE DATABASE IF NOT EXISTS test");
这里创建了一个名为test的数据库。
查询数据
在mysql中查询数据,需要使用到mysql中的SELECT语句。在C++中,可以使用sql::ResultSet对象来保存查询结果。
try {
/* 建立连接 */
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *stmt;
sql::ResultSet *res;
driver = get_driver_instance();
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
stmt = conn->createStatement();
/* 查询数据 */
res = stmt->executeQuery("SELECT * FROM user");
while (res->next()) {
cout << "id: " << res->getInt("id") << endl;
cout << "name: " << res->getString("name") << endl;
cout << "age: " << res->getInt("age") << endl;
cout << endl;
}
/* 关闭连接 */
delete res;
delete stmt;
delete conn;
} 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;
}
- 查询语句:
SELECT * FROM user
这里查询了user表中的所有数据。
- 使用res对象保存查询结果:
res = stmt->executeQuery("SELECT * FROM user");
- 遍历结果集并输出:
while (res->next()) {
cout << "id: " << res->getInt("id") << endl;
cout << "name: " << res->getString("name") << endl;
cout << "age: " << res->getInt("age") << endl;
cout << endl;
}
这里使用了res->getInt("id")、res->getString("name")和res->getInt("age")来获取查询结果中的数据。
以上就是使用C++连接mysql时遇到问题的解决方案,希望能对你有所帮助。
示例1:
- 首先,我们需要在mysql中创建一个database和一个table,用来存储我们的数据。
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE IF NOT EXISTS student (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
age INT NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO student (name, age) VALUES ("Tom", 18);
INSERT INTO student (name, age) VALUES ("Jerry", 19);
- 然后,在C++中连接mysql并查询student表中的所有数据。
cpp
try {
/* 建立连接 */
sql::Driver* driver;
sql::Connection* conn;
sql::Statement* stmt;
sql::ResultSet* res;
driver = get_driver_instance();
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
stmt = conn->createStatement();
/* 查询数据 */
res = stmt->executeQuery("SELECT * FROM student");
while (res->next()) {
cout << "id: " << res->getInt("id") << endl;
cout << "name: " << res->getString("name") << endl;
cout << "age: " << res->getInt("age") << endl;
cout << endl;
}
/* 关闭连接 */
delete res;
delete stmt;
delete conn;
}
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;
}
- 编译运行,输出结果:
```
id: 1
name: Tom
age: 18
id: 2
name: Jerry
age: 19
```
示例2:
- 我们还可以使用C++向mysql中插入一条数据。
cpp
try {
/* 建立连接 */
sql::Driver* driver;
sql::Connection* conn;
sql::Statement* stmt;
driver = get_driver_instance();
conn = driver->connect("tcp://127.0.0.1:3306", "root", "password");
stmt = conn->createStatement();
/* 插入数据 */
stmt->execute("INSERT INTO student (name, age) VALUES ('Lisa', 20)");
/* 查询数据 */
sql::ResultSet* res = stmt->executeQuery("SELECT * FROM student");
while (res->next()) {
cout << "id: " << res->getInt("id") << endl;
cout << "name: " << res->getString("name") << endl;
cout << "age: " << res->getInt("age") << endl;
cout << endl;
}
/* 关闭连接 */
delete res;
delete stmt;
delete conn;
}
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;
}
- 编译运行,输出结果:
```
id: 1
name: Tom
age: 18
id: 2
name: Jerry
age: 19
id: 3
name: Lisa
age: 20
```
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C++与mysql连接遇到的问题汇总 - Python技术站