Arm Qt 实战二:在Arm设备中保存sql数据

1、为项目添加sql支持

首先确定你的Arm设备中已经有了Sql库的支持。
在项目的.pro文件中增加QT += sql即可调用QT的sqlite

#-------------------------------------------------
#
# Project created by QtCreator 2020-08-01T19:53:32
#
#-------------------------------------------------

QT       += core
QT       += network
QT       += sql
QT       -= gui

TARGET = Core
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    appData.cpp \
    core/siemensS7/siemensS7.cpp \
    util/plcUtil.cpp \
    core/siemensS7/siemensS7Register.cpp \

2、公共驱动类Sql

sql.cpp

#include "sql.h"

Sql::Sql(QObject *parent) :
    QObject(parent)
{
    sqlFilePath = "/home/root/sql/multiFactor.db";
}

bool Sql::createSqlDatabaseFactory()
{
    printf("Sql::新增数据库\r\n");
    qSqlDatabase=QSqlDatabase::addDatabase("QSQLITE");
    qSqlDatabase.setDatabaseName(sqlFilePath);
    if(!qSqlDatabase.open())
    {
        printf("Sql::Sql不能创建\r\n");
        return false;
    }
    return true;
}

bool Sql::exec(QString query)
{
    QSqlQuery qSqlQuery;
    //QString exec = "CREATE TABLE IF NOT EXISTS 'powerOnLog' (id integer primary key autoincrement,	deviceId VARCHAR(5), powerOn VARCHAR(50), timestamp VARCHAR(20));";
    printf("PowerOnSql::exec = %s\r\n",query.toLatin1().data());
    if(qSqlQuery.exec(query) == false)
    {
        printf("Sql::语句执行失败\r\n");
        return false;
    }
    else
    {
        printf("Sql::语句执行成功\r\n");
        return true;
    }
}

sql.h

#ifndef SQL_H
#define SQL_H

#include <QObject>
#include <QtSql>
#include <QMutex>

class Sql : public QObject
{
    Q_OBJECT
public:
    explicit Sql(QObject *parent = 0);
    bool createSqlDatabaseFactory();
    bool exec(QString query);

private:
    QString sqlFilePath;
    QMutex qMutex;
    QSqlDatabase qSqlDatabase;

signals:

public slots:

};

#endif // SQL_H

3、简单使用Sql工具类进行数据库操作

简单写一个记录设备何时开机、并且如果开机则定时写入一条数据的线程。

powerlogthread.cpp
AppData::是我写的一个全局静态变量,你也可以直接new一个Sql类进行操作。灵活一点。

#include "powerlogthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <QDateTime>

#include "../appData.h"

PowerlogThread::PowerlogThread(QObject *parent) :
    QThread(parent)
{
    moveToThread(this);
    this->start();
}
void PowerlogThread::run()
{
    printf("PowerlogThread::run\r\n");

    /*创建数据表*/
    qMutex.unlock();
    AppData::sql.createSqlDatabaseFactory();
    AppData::sql.exec(createDatabase());
    AppData::sql.exec(insertPowerOn());
    qMutex.lock();

    while(true)
    {
        this->msleep(5000);

        qMutex.unlock();
        AppData::sql.exec(insertPowerKeep());
        qMutex.lock();
    }
}
QString PowerlogThread::createDatabase()
{
    QString exec = "CREATE TABLE IF NOT EXISTS 'powerOnLog' (id integer primary key autoincrement,	deviceId VARCHAR(5), powerOn VARCHAR(50), timestamp VARCHAR(20));";

    return exec;
}
QString PowerlogThread::insertPowerOn()
{
    QDateTime current_date_time = QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss");

    QString sql = "insert into 'powerOnLog'(deviceId,powerOn,timestamp) values ('001','poweron',"
            +tr("'")+current_date+tr("'")+
        +")";
    return sql;
}
QString PowerlogThread::insertPowerKeep()
{
    QDateTime current_date_time = QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss");

    QString sql = "insert into 'powerOnLog'(deviceId,powerOn,timestamp) values ('001','keep',"
            +tr("'")+current_date+tr("'")+
        +")";
    return sql;
}

powerlogthread.h

#ifndef POWERLOGTHREAD_H
#define POWERLOGTHREAD_H

#include <QThread>
#include <QMutex>
#include <QtSql>

class PowerlogThread : public QThread
{
    Q_OBJECT
public:
    explicit PowerlogThread(QObject *parent = 0);

private:
    void run();
    QMutex qMutex;


    QString createDatabase();
    QString insertPowerOn();
    QString insertPowerKeep();

signals:

public slots:

};

#endif // POWERLOGTHREAD_H

4、查看运行结果

自动创建了powerOnLog数据表

在这里插入图片描述

程序启动写入poweron,每隔5秒写入了一条keep数据,

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值