以下是一个使用C++ QT连接SQL Server的示例代码:
#include <QCoreApplication>
#include <QDebug>
#include <QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 创建数据库连接
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};SERVER=<server name>;DATABASE=<database name>;UID=<user name>;PWD=<password>");
// 打开数据库连接
if (!db.open()) {
qDebug() << "Failed to open database:" << db.lastError().text();
return 1;
} else {
qDebug() << "Database connected successfully.";
}
// 执行SQL语句
QSqlQuery query;
query.exec("SELECT * FROM <table name>");
while (query.next()) {
QString name = query.value(0).toString();
QString age = query.value(1).toString();
qDebug() << name << age;
}
// 关闭数据库连接
db.close();
return a.exec();
}
需要将`<server name>`、`<database name>`、`<user name>`和`<password>`替换为实际的值,以正确连接SQL Server数据库。同时,需要在项目文件(.pro)中添加以下代码:
QT += sql
以确保QT能够使用ODBC驱动程序来连接SQL Server。
C++ QT连接SQL Server还支持一些高级用法,例如:
1. 使用预处理语句
预处理语句可以提高SQL语句的执行效率,并且可以防止SQL注入攻击。可以使用QSqlQuery::prepare()方法来创建预处理语句,例如:
QSqlQuery query;
query.prepare("SELECT * FROM <table name> WHERE name = ?");
query.addBindValue("John");
query.exec();
while (query.next()) {
QString name = query.value(0).toString();
QString age = query.value(1).toString();
qDebug() << name << age;
}
其中,`?`表示占位符,可以使用`addBindValue()`方法来设置占位符的值。
2. 使用事务
事务可以保证一组SQL语句的原子性,即要么全部执行成功,要么全部执行失败。可以使用QSqlDatabase::transaction()和QSqlDatabase::commit()方法来实现事务,例如:
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={SQL Server};SERVER=<server name>;DATABASE=<database name>;UID=<user name>;PWD=<password>");
if (!db.open()) {
qDebug() << "Failed to open database:" << db.lastError().text();
return 1;
} else {
qDebug() << "Database connected successfully.";
}
QSqlQuery query(db);
db.transaction(); // 开始事务
query.exec("INSERT INTO <table name> (name, age) VALUES ('John', 30)");
query.exec("INSERT INTO <table name> (name, age) VALUES ('Mary', 25)");
if (query.lastError().isValid()) {
qDebug() << "Failed to execute SQL:" << query.lastError().text();
db.rollback(); // 回滚事务
} else {
db.commit(); // 提交事务
}
db.close();
在执行多个SQL语句时,需要在事务中执行,以确保这些SQL语句的原子性。
3. 使用模型视图框架
QT提供了模型视图框架,可以方便地将SQL查询结果显示在QT的界面上。可以使用QSqlTableModel、QSqlRelationalTableModel和QSqlQueryModel等类来实现模型视图功能,例如:
QSqlTableModel *model = new QSqlTableModel(parent, db);
model->setTable("<table name>");
model->select();
QTableView *view = new QTableView(parent);
view->setModel(model);
view->show();
其中,`<table name>`是要显示的表的名称。使用QSqlTableModel可以将表格数据显示在QTableView控件中。/-------------------------------------------------------------------
以下是一个C++ QT连接SQL Server的工具类,包含增删改查的示例代码:
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
class SQLServerConnection {
public:
SQLServerConnection(const QString& serverName, const QString& dbName, const QString& userName, const QString& password) :
m_serverName(serverName), m_dbName(dbName), m_userName(userName), m_password(password)
{
m_db = QSqlDatabase::addDatabase("QODBC");
m_db.setDatabaseName(QString("DRIVER={SQL Server};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;").arg(m_serverName).arg(m_dbName).arg(m_userName).arg(m_password));
}
bool open() {
if (m_db.open()) {
return true;
}
else {
qDebug() << "Failed to connect to SQL Server:" << m_db.lastError().text();
return false;
}
}
void close() {
m_db.close();
}
bool executeNonQuery(const QString& query) {
QSqlQuery q(m_db);
if (q.exec(query)) {
return true;
}
else {
qDebug() << "Failed to execute query:" << q.lastError().text();
return false;
}
}
QSqlQuery executeQuery(const QString& query) {
QSqlQuery q(m_db);
if (q.exec(query)) {
return q;
}
else {
qDebug() << "Failed to execute query:" << q.lastError().text();
return QSqlQuery();
}
}
bool insert(const QString& tableName, const QStringList& columns, const QStringList& values) {
QString query = QString("INSERT INTO %1 (%2) VALUES (%3)").arg(tableName).arg(columns.join(", ")).arg(values.join(", "));
return executeNonQuery(query);
}
bool update(const QString& tableName, const QString& setClause, const QString& whereClause = "") {
QString query = QString("UPDATE %1 SET %2").arg(tableName).arg(setClause);
if (!whereClause.isEmpty()) {
query += " WHERE " + whereClause;
}
return executeNonQuery(query);
}
bool remove(const QString& tableName, const QString& whereClause) {
QString query = QString("DELETE FROM %1 WHERE %2").arg(tableName).arg(whereClause);
return executeNonQuery(query);
}
QSqlQuery select(const QString& tableName, const QStringList& columns, const QString& whereClause = "") {
QString query = QString("SELECT %1 FROM %2").arg(columns.join(", ")).arg(tableName);
if (!whereClause.isEmpty()) {
query += " WHERE " + whereClause;
}
return executeQuery(query);
}
private:
QString m_serverName;
QString m_dbName;
QString m_userName;
QString m_password;
QSqlDatabase m_db;
};
使用方法:
SQLServerConnection conn("localhost", "mydb", "myuser", "mypassword");
if (conn.open()) {
// insert
QStringList columns = {"name", "age", "gender"};
QStringList values = {"'John'", "25", "'M'"};
conn.insert("mytable", columns, values);
// update
QString setClause = "age = 30, gender = 'F'";
QString whereClause = "name = 'John'";
conn.update("mytable", setClause, whereClause);
// delete
QString whereClause = "age > 30";
conn.remove("mytable", whereClause);
// select
QStringList columns = {"name", "age", "gender"};
QString whereClause = "age >= 25";
QSqlQuery q = conn.select("mytable", columns, whereClause);
while (q.next()) {
QString name = q.value(0).toString();
int age = q.value(1).toInt();
QString gender = q.value(2).toString();
// process result
}
conn.close();
}