以下是一个C++ QT操作MySQL数据库的工具类示例代码,包括连接数据库、创建表、插入数据、删除数据、修改数据、查询数据、查询表字段名称等操作:
#include
#include
#include
#include
class MysqlTool
{
public:
MysqlTool(const QString& host, int port, const QString& dbname, const QString& user, const QString& password)
{
// 连接MySQL数据库
db_ = QSqlDatabase::addDatabase("QMYSQL");
db_.setHostName(host);
db_.setPort(port);
db_.setDatabaseName(dbname);
db_.setUserName(user);
db_.setPassword(password);
if (!db_.open()) {
qDebug() << "Failed to connect to database:" << db_.lastError().text();
}
}
~MysqlTool()
{
// 关闭数据库连接
db_.close();
}
bool createTable(const QString& tableName, const QStringList& fields)
{
QString sql = QString("CREATE TABLE IF NOT EXISTS %1 (%2)").arg(tableName).arg(fields.join(", "));
QSqlQuery query;
if (!query.exec(sql)) {
qDebug() << "Failed to create table:" << query.lastError().text();
return false;
}
return true;
}
bool insertData(const QString& tableName, const QStringList& fields, const QVariantList& values)
{
QString sql = QString("INSERT INTO %1 (%2) VALUES (%3)").arg(tableName).arg(fields.join(", ")).arg(values.join(", "));
QSqlQuery query;
if (!query.exec(sql)) {
qDebug() << "Failed to insert data:" << query.lastError().text();
return false;
}
return true;
}
bool deleteData(const QString& tableName, const QString& condition)
{
QString sql = QString("DELETE FROM %1 WHERE %2").arg(tableName).arg(condition);
QSqlQuery query;
if (!query.exec(sql)) {
qDebug() << "Failed to delete data:" << query.lastError().text();
return false;
}
return true;
}
bool updateData(const QString& tableName, const QString& setClause, const QString& condition)
{
QString sql = QString("UPDATE %1 SET %2 WHERE %3").arg(tableName).arg(setClause).arg(condition);
QSqlQuery query;
if (!query.exec(sql)) {
qDebug() << "Failed to update data:" << query.lastError().text();
return false;
}
return true;
}
bool selectData(const QString& tableName, const QStringList& fields, const QString& condition, QList
{
QString sql = QString("SELECT %1 FROM %2 WHERE %3").arg(fields.join(", ")).arg(tableName).arg(condition);
QSqlQuery query;
if (!query.exec(sql)) {
qDebug() << "Failed to select data:" << query.lastError().text();
return false;
}
while (query.next()) {
QVariantList record;
for (int i = 0; i < fields.size(); ++i) {
record.append(query.value(i));
}
result.append(record);
}
return true;
}
QStringList getFields(const QString& tableName)
{
QStringList result;
QSqlRecord record = db_.record(tableName);
for (int i = 0; i < record.count(); ++i) {
result.append(record.fieldName(i));
}
return result;
}
private:
QSqlDatabase db_;
};
使用示例:
int main()
{
MysqlTool mysql("localhost", 3306, "testdb", "root", "password");
// 创建表
QStringList fields = {"id INT PRIMARY KEY", "name VARCHAR(50)", "age INT"};
mysql.createTable("students", fields);
// 插入数据
QStringList insertFields = {"id", "name", "age"};
QVariantList insertValues = {1, "张三", 20};
mysql.insertData("students", insertFields, insertValues);
// 查询数据
QStringList selectFields = {"id", "name", "age"};
QList
mysql.selectData("students", selectFields, "age > 18", selectResult);
for (const auto& record : selectResult) {
int id = record[0].toInt();
QString name = record[1].toString();
int age = record[2].toInt();
qDebug() << "id:" << id << "name:" << name << "age:" << age;
}
// 更新数据
QString setClause = "age = 25";
QString condition = "id = 1";
mysql.updateData("students", setClause, condition);
// 删除数据
QString deleteCondition = "age < 18";
mysql.deleteData("students", deleteCondition);
// 查询表字段名称
QStringList fields = mysql.getFields("students");
qDebug() << "Fields:" << fields;
return 0;
}
以上代码仅供参考,实际使用时需要根据实际情况进行修改。
需要注意的是,使用QT操作MySQL数据库需要在项目文件中添加以下依赖项:
QT += sql
同时,也需要确保安装了MySQL数据库驱动程序,可以使用以下命令安装:
sudo apt-get install libqt5sql5-mysql