QT操作SQLite数据库方法1

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txwtech笛克特科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值