Summary of Qt's database query problems (query variable time data)

Database usage: QSQLITE

Database creation:

1) Create database table data type 

2) Save test data (.CSV file data import)

3) tabview displays table data

4) Query data according to conditions


Summary of problems encountered:

It mainly appears in the database query part, but in the final analysis it is a problem of the database time type (varchar)

The database table at the beginning is:


It can be seen that when the time data type is varchar, the data storage format is 2018/4/28, which is yyyy/m/dd, yyyy/m/dd, yyyy/mm/d, yyyy/mm/dd four formats. This is where the problem lies. Since the statement in the database query uses QString string matching, the code is

   char *ch;
   QSqlQueryModel *mod = new QSqlQueryModel(this);
   QDate dateEdit=ui->dateEdit->date();
   QString date=dateEdit.toString("yyyy/MM/dd");
   qDebug()<<"日期:"<<date;
   QByteArray ba=date.toLatin1();
   ch = ba.data ();
   qDebug()<<"日期:"<<ch;
  mod->setQuery(QString("select * from hdata1 where time = '%1'").arg(ch));
   qDebug()<<"转换"<<QString("select * from hdata where time ='%1'").arg(ch);
   if (mod->lastError().isValid())
       qDebug() << mod->lastError();
   ui->tableView->setModel(mod);
 
 

There is a problem here. When we actually query, we will find that the query is this:

Date: "2018/04/29"

Date: 2018/04/29

转换 "select * from hdata where time ='2018/04/29'" 

At first, I didn't notice the difference in time, but after thinking about it, it has something to do with the time type varchar, which is matched by characters.

So when matching by character, '2018/4/29' is not the same as '2018/04/29'.

This is the last question that comes to my mind. A little bit of a problem makes me doubt myself. I really can't ignore the details!

In the end, the code statement was not changed, the database content was changed, and it was found out at once!

search result:

Of course, encountering a problem not only solves the problem, but also deepens my understanding of other problems;

First, it is different database data types, how to choose the type of different data types, the time in this article is better to use date, and the use of datetime in mysql will automatically convert, instead of designing the problem of characters here. At the beginning, there was also a character error. Although the processing method was correct, the database type was still inconsistent, which led to unawareness.

void HData::on_pushButton_clicked()
{
    char *ch0;
    char *ch1;
    char *ch2;
    QSqlQueryModel *mod = new QSqlQueryModel(this);
    QDate dateEdit=ui->dateEdit->date();
    QString date=dateEdit.toString("yyyy/MM/dd");
    QStringList list = date.split("/");//QString string split function
//    qDebug()<<"日期:"<<date;
//    QByteArray ba=date.toLatin1();
// ch = ba.data ();
//    qDebug()<<"日期:"<<ch;
//        qDebug()<<list[i]<<endl;
        QByteArray ba0=list[0].toLatin1();
        QByteArray ba1=list[1].toLatin1();
        QByteArray ba2=list[2].toLatin1();
        ch0 = ba0.data ();
        qDebug()<<ch0;
        ch1 = ba1.data ();
        qDebug()<<ch1;
        ch2=ba2.data();
        qDebug()<<ch2;
 
 
    mod->setQuery(QString("select * from hdata where time=%1/%2/%3").arg(ch0).arg(ch1).arg(ch2));
    if (mod->lastError().isValid())
        qDebug() << mod->lastError();
    ui->tableView->setModel(mod);
 
 
}
 
 

Have a deep understanding of the segmentation of QString strings!

第二、对数据库数据段时间查询,有了深入的理解,同时知道了数据库这方面还是有点薄弱(不太常用数据库),下面就需要搞基本数据库的书,结合项目深入探索一下,很爽的感觉!(方向:分布式数据库技术要掌握)

一些基本的段时间查询语句总结如下:

select * from tongji where [销售时间]=date('now','localtime') --当天
select * from tongji where strftime('%Y%m',[销售时间])=strftime('%Y%m','now','localtime')  --当月
select * from tongji where strftime('%Y',[销售时间])=strftime('%Y','now','localtime')  --当年
 
 

对于项目中取变量数据查询,需要QString匹配:

setQuery(QString("select * from hdata1 where time = '%1'").arg(ch));
 
 

QString SqlStr = QString("time > '%1' and time < '%2'").arg(startDateTime).arg(endDateTime);
QString SqlStr = QString("select * from hdata where time between '%1' and '2%'").arg(startDateTime).arg(endDateTime);
model->setFilter(SqlStr);//按这个方式查询
model->select();
 
 

第三,QSQLITE数据类型(摘取一些)




获取的时间函数date():



写了这篇文章,目的是也希望遇到同样问题的朋友,有一个参考解决的方法!

不至于像我这样摸索半天,浪费时间!毕竟时间很宝贵!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324849723&siteId=291194637