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

Posted 秦晓宇

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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数据,

以上是关于Arm Qt 实战二:在Arm设备中保存sql数据的主要内容,如果未能解决你的问题,请参考以下文章

Arm Qt 实战二:使用libmodbus创建MobusRtu-modbus_new_rtu为NULL的问题

带有随机密码和密码的 SQL Server ARM 模板保存到 Key Vault

iTOP-IMX6UL 实战项目:ssh 服务器移植到 arm 开发板

一个基于Qt的截屏程序

如何将用QT做好的播放器移植到ARM开发板上

HMI-65-MeterDisplay for Arm Linux液晶仪表Arm Linxu迁移环境配置