Qt5 使用 SQLite (1) – 控制台程序

目录 Content
[hide]

在上篇文章《Sqlite3 的 shell 使用》中初步了解了 SQLite Shell 的使用,本篇则探索在 Qt5 中如何使用 SQLIte。Qt 号称 “Write once, compile anywhere”,简写为WOCA ,即“一次编写到处编译”。

一、控制台程序初步尝试

1. 新建项目

使用Qt5新项目向导建立一个 Qt Console Application 程序,取名为 qt5-sqlite。在 C++ 文件里包含 #include <QtSql> 在 .pro 文件里加入 QT += sql 。具体参见帮助文件 Qt SQL 部分。

#include <QCoreApplication>
#include <QtSql>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    return a.exec();
}

.pro 文件

#-------------------------------------------------
#
# Project created by QtCreator 2014-12-19T11:15:27
#
#-------------------------------------------------

QT       += core sql

QT       -= gui

TARGET = qt5-sqlite
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

2.代码实现

#include <QCoreApplication>
#include <QtSql>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    // add QSQLITE driver
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    // if not exist, will create a new one
    db.setDatabaseName("data.db");
    if(!db.open()){
        qDebug()<<"Could not open file!"<<endl;
        return -1;
    }else{
        qDebug()<<"Open file: "<<db.databaseName()<<" sucessfully!"<<endl;
    }
    // create a new table
    QSqlQuery query;
    if(query.exec("create table if not exists product (type TEXT, para1 REAL, para2 REAL, para3 REAL, timestamp NOT NULL DEFAULT  (datetime('now','localtime')))")){
        qDebug()<<"creat table: product successfully!"<<endl;
    }else{
        qDebug()<<"could not creat table: product!"<<endl;
    }
    //insert product1
    if(query.exec(" insert into product(type, para1, para2, para3)  values ('SN001', 30.00,10.00, 20.00)")){
        qDebug()<<"insert  product successfully!"<<endl;
    }else{
        qDebug()<<"could not insert product!"<<endl;
    }
    //insert product2
    if(query.exec("insert into product(type, para1, para2, para3)  values ('SN002', 29.01,15.00, 18.00)")){
        qDebug()<<"insert  product successfully!"<<endl;
    }else{
        qDebug()<<"could not insert product!"<<endl;
    }
    //insert product3
    if(query.exec("insert into product(type, para1, para2, para3)  values ('SN003', 35.10,10.01, 20.0111)")){
        qDebug()<<"insert  product successfully!"<<endl;
    }else{
        qDebug()<<"could not insert product!"<<endl;
    }
    //query all data
    if(query.exec("select * from product")){
        qDebug()<<"begin query all!"<<endl;
    }else{
        qDebug()<<"could not query!"<<endl;
    }
    //output result
    while (query.next()){
        qDebug()<<""<<query.value(0).toString()<<""<<query.value(1).toString()<<""<<query.value(2).toString()<<""<<query.value(3).toString()<<""<<query.value(4).toString()<<endl;
    }
    db.close();
    return a.exec();
}

效果如图:

qt5-sqlite源码下载

扩展阅读

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.