专业的盐城网站开发,wordpress如和安装,python用于网站开发,做外单都有什么网站一.Qt为SQL数据库提供支持的基本模块#xff08;Qt SQL#xff09; Qt SQL的API分为不同层#xff1a; 驱动层 SQL API层 用户接口层 1.驱动层 对于Qt 是基于C来实现的框架#xff0c;该层主要包括QSqlDriver#xff0c;QSqlDriverCreator,QSqlDriverCreatorBase,QSqlPlug…一.Qt为SQL数据库提供支持的基本模块Qt SQL Qt SQL的API分为不同层 驱动层 SQL API层 用户接口层 1.驱动层 对于Qt 是基于C来实现的框架该层主要包括QSqlDriverQSqlDriverCreator,QSqlDriverCreatorBase,QSqlPlugin,and QSqlResult.这一层提供了特定数据和SQLAPI层之间的底层桥梁。 2.SQL API层 对于SQL API层提供了数据库的访问相关类其中QSqlDatabase类进行连接QSlqQuery可以完成于数据库的交互。除此之外包括了还提供了QSqlErrorQSqlFieldQSqlIndexand QSqlRecord类。 3.用户接口层 用户接口层的几个类实现了将数据库中的数据链连接到窗口部件上这些类是使用模型/试图框架实现的它们是更高层次的抽象主要包括QSqlQueryModel,QSqlTableModelandQSqlRelationalTableModel. 4.Qt SQL模块对数据库类
在Qt中为SQL数据库提供驱动程序层、SQL API层和用户界面其提供主要类的简要功能说明见下表 二.SQLite数据库操作流程 第一步在项目管理文件(.pro)中增加数据库模块QT sql 第二步查看Qt对数据库的驱动的类型的支持 第三步连接数据库打开数据库 第四步访问数据库读写操作 第五步关闭数据库 三.代码示例
1.查看Qt对数据库的驱动的类型的支持
代码
#include QApplication
#include QSqlDatabase
#include QDebug
#include QStringList int main(int argc, char *argv[])
{ QApplication a(argc, argv); qDebug() Available drivers:; QStringList drivers QSqlDatabase::drivers(); foreach(QString driver, drivers) qDebug() driver; return a.exec();
}
2执行结果 2.Qt读写Sqlite数据库
Qt访问Sqlite数据库的三种方式(即使用三种类库去访问)分别为QSqlQuery、QSqlQueryModel、QSqlTableModel对于这三种类库可看为一个比一个上层也就是封装的更厉害甚至第三种QSqlTableModel根本就不需要开发者懂SQL语言也能操作Sqlite数据库。
下面示例采用QSqlQuery方法实现数据库操作。
1widget.h
#ifndef WIDGET_H
#define WIDGET_H #include QWidget
#include QSqlError
#include QSqlQueryModel
#include QSqlRelationalTableModel
#include QSqlRelationalTableModel
#include QTableView namespace Ui {
class Widget;
} class Widget : public QWidget
{ Q_OBJECT public: explicit Widget(QWidget *parent nullptr); ~Widget(); private: Ui::Widget *ui; QSqlTableModel *model;
}; #endif // WIDGET_H 2widget.cpp
#include widget.h
#include ui_widget.h
#include QSqlDatabase
#include QDebug
#include QSqlQuery Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{ ui-setupUi(this); //1.创建与打开数据库 QSqlDatabase db QSqlDatabase::addDatabase(QSQLITE); db.setDatabaseName(dataset.db); if(!db.open()) { qDebug() 创建失败 db.lastError(); } else { qDebug() 创建成功; } //2.创建表 QSqlQuery sql_query; QString create create table student(id int,name varchar(20),age int); sql_query.prepare(create); sql_query.exec(); //32.插入数据 QString insert_sql insert into student values(?,?,?) ; sql_query.prepare(insert_sql); sql_query.addBindValue(1); sql_query.addBindValue(Tom); sql_query.addBindValue(15); if(!sql_query.exec()) { qDebug() 插入执行错误 sql_query.lastError(); } else { qDebug() 插入成功; } //4.读取数据库表全部内容 QString select_sql select * from student; sql_query.prepare(select_sql); if(!sql_query.exec()) { qDebug() 查看执行错误 sql_query.lastError(); } else { while(sql_query.next())//如果下一行数据还存在就继续执行 { int id sql_query.value(0).toInt();//将sql里的int转换为qt里的int QString name sql_query.value(1).toString();//将sql里的string转化为qt里的string int age sql_query.value(2).toInt(); qDebug() id id name: name age: age; } } //5.更新数据内容 QString update_sql update student set name :nm where id :n; sql_query.prepare(update_sql); sql_query.bindValue(:nm,TTTs); sql_query.bindValue(:n, 1); if(!sql_query.exec()) { qDebug() 更新失败 sql_query.lastError(); } else { qDebug() 更新成功; } //6.再次读取数据内容 select_sql select * from student; sql_query.prepare(select_sql); if(!sql_query.exec()) { qDebug() 查看执行错误 sql_query.lastError(); } else { while(sql_query.next())//如果下一行数据还存在就继续执行 { int id1 sql_query.value(0).toInt();//将sql里的int转换为qt里的int QString name1 sql_query.value(1).toString();//将sql里的string转化为qt里的string int age1 sql_query.value(2).toInt(); qDebug() id id1 name: name1 age: age1; } } //7.删除数据内容 QString delete1_sql delete from student where id 1; // QString delete1_sql delete from student; sql_query.prepare(delete1_sql); if(!sql_query.exec()) { qDebug() 删除失败; qDebug() sql_query.lastError().text(); } else { qDebug() 删除成功; }
} Widget::~Widget()
{ delete ui;
} 3执行结果