QT操作SQLite数据库方法1
// 驱动层为具体的数据库和SQL接口层之间提供了底层的桥梁;SQL接口层提供了对数据库的访问,
//其中的QSqlDatabase类用来创建连接,QSqlQuery类可以使用SQL语句来实现与数据库交互,
//其他几个类对该层提供了支持;用户接口层的几个类实现了将数据库中的数据链接到窗口部件上,
//这些类是使用前一章的模型/视图框架实现的,它们是更高层次的抽象,即便不熟悉SQL也可以操作数据库。
//如果要使用QtQql模块中的这些类,需要在项目文件(.pro文件)中添加QT += sql这一行代码。对应数据库部分的内容,大家可以在帮助中查看SQL Programming关键字。
main.cpp
#include <QCoreApplication>
#include "sql_connection.h"
#include <QDebug>
#include <QVariant>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//创建数据库的连接
qDebug()<<"开始连接";
if(!createSqlCon())
{
qDebug()<<"创建连接失败";
return 1;
}
QSqlQuery query;//打开默认的连接
query.exec("select * from student");//开始查询
while(query.next())
{
qDebug()<<query.value(0).toInt()<<query.value(1).toString();
}
return a.exec();
}
头文件:
#ifndef SQL_CONNECTION_H
#define SQL_CONNECTION_H
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QSqlQuery>
//'QMessageBox' file not found,pro里面添加:QT += sql widgets
//by txwtech
// 其中驱动层为具体的数据库和SQL接口层之间提供了底层的桥梁;SQL接口层提供了对数据库的访问,
//其中的QSqlDatabase类用来创建连接,QSqlQuery类可以使用SQL语句来实现与数据库交互,
//其他几个类对该层提供了支持;用户接口层的几个类实现了将数据库中的数据链接到窗口部件上,
//这些类是使用前一章的模型/视图框架实现的,它们是更高层次的抽象,即便不熟悉SQL也可以操作数据库。
//如果要使用QtQql模块中的这些类,需要在项目文件(.pro文件)中添加QT += sql这一行代码。对应数据库部分的内容,大家可以在帮助中查看SQL Programming关键字。
// 这里要重点提一下SQLite数据库,它是一款轻型的文件型数据库,
//主要应用于嵌入式领域,支持跨平台,而且Qt对它提供了很好的默认支持,
//by txwtech
static bool createSqlCon()
{
QSqlDatabase sql_db=QSqlDatabase::addDatabase("QSQLITE");//创建默认连接-一个没有命名的连接
QSqlDatabase sql_db2=QSqlDatabase::addDatabase("QSQLITE","db1");//创建指定的db1连接名字
sql_db.setDatabaseName(":memory:");//表示数据库在运行期间有效,程序结束自动撤销,不会创建数据库名字
//sql_db.setDatabaseName("tt.db");//在项目中创建tt.db数据库
if(!sql_db.open())
{
QMessageBox::critical(0,"无法打开数据库","不能创建数据库",QMessageBox::Cancel);
return false;
}
QSqlQuery query;//没有指定连接名字,就是默认连接名
QSqlQuery query1(sql_db2);//打开自定数据库连接对象by txwtech
query.exec("create table student(id int primary key,"
"name varchar(20))"); //创建表时,换行写,直接是双引号连接语句, p383
query.exec("insert into student values(0,'bob')");//插入数据
query.exec("insert into student values(1,'bian')");//插入数据
query.exec("insert into student values(2,'machiel')");//插入数据
return true;
}
#endif // SQL_CONNECTION_H
pro
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
QT+=sql
QT += sql widgets
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
sql_connection.h