#include "databasethread.h"


DatabaseThread::DatabaseThread(QObject *parent, QSqlDatabase *db) :
    QThread(parent)
{
    Db = db;
    hour = 255;
    keep=false;
    sqlList.clear();

}

void DatabaseThread::appendSql(QString sql)
{
    sqlList.append(sql);
}

void DatabaseThread::stop()
{
    keep = false;
}

void DatabaseThread::run()
{
    keep = true;
    while (keep) {

        if(sqlList.length()>0)
        {
            if(Db->transaction())
            {
                while(sqlList.length()>0)
                {
                    emit dbdata_log(QString("[%1] do sql: \"%2\"\r\n")
                             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
                             .arg(sqlList.first()));
                    Db->exec(sqlList.first());
                    sqlList.removeFirst();
                    usleep(10000);
                }
                Db->commit();
            }
        }
        usleep(50000);
    }


}











