SQL Data type error



  • Hello guys!

    I have a problem with SQL Data for participants which is not being displayed correctly. I need to show the date & time (2016-04-16T16:22:32.604Z) which is a QDateTime and it shows itself up as "00:54:36.848" & a few Participant tags which are in hex value format and thus a QString but it is displayed as an integer.
    I create my table in the ":memory" and the table code is as follows

    qry.prepare("CREATE TABLE IF NOT EXISTS participant ("
                    "participant_id INTEGER PRIMARY KEY,"
                    "firstname VARCHAR(10),"
                    "lastname  VARCHAR(20),"
                    "participant_tag  VARCHAR(20)"
                    "participant_time  TIMESTAMP"
                  ")");
    

    This is how I am writing to the table

    QSqlQuery qry;
        qry.prepare("insert into participant(firstname, lastname, participant_tag, participant_time)"
                    " values(:firstname,:lastname, :participant_tag, :participant_time)");
        qry.bindValue(":firstname", firstname);
        qry.bindValue(":lastname", lastname);
        qry.bindValue(":participant_tag", participant_tag);
        qry.bindValue(":participant_time", participant_time);
    

    This is how I am displaying in the QTableView

    QStandardItemModel *model = new QStandardItemModel(station1data.size(), 2,this);
       model->setHorizontalHeaderItem(0, new QStandardItem(QString("participant_id")));
       model->setHorizontalHeaderItem(1, new QStandardItem(QString("participant_tag")));
       model->setHorizontalHeaderItem(2, new QStandardItem(QString("participant_time")));
       int n = 0;
       foreach(msg_station1data d, station1data) {
           QStandardItem *item = new QStandardItem(QString(d.participant_id));
           model->setItem(n,0,item);
           item = new QStandardItem(QString(d.participant_tag));
           model->setItem(n,1,item);
           item = new QStandardItem((d.participant_time).toString("hh:mm:ss.zzz"));
           model->setItem(n,2,item);
           ++n;
    

    Any help suggestion is appreciated (Thanks!)


  • Lifetime Qt Champion

    Hi,

    For the date part, you are currently seeing what you are asking for (i.e. your format string).Take a look at the .toString funtion call.

    For the hex part, can you show an example ?



  • Thats true but I want "16:22:32.604Z" as I want to plot this later. The hex value that i send is like "a2b3c124d" but i get it back as integer value "1" "2" "3" for the first second and third values received but not the actual HEX values.


  • Lifetime Qt Champion

    Then store the QDateTime value and create a small QStyledItemDelegate to show the date the way you want it.

    How are you storing the hex values in the first place ?



  • I store the HEX in the table as follows:

     "participant_tag  VARCHAR(20)"
     "participant_time  TIMESTAMP"
    


  • @SGaist said:

    Then store the QDateTime value and create a small QStyledItemDelegate to show the date the way you want it.

    Is there any reason for this ? can't i just display it otherwise as a string ?


  • Lifetime Qt Champion

    You are writing that you want to show it like 2016-04-16T16:22:32.604Z but later re-use it in another format. So either you store the QDateTime object and so you can re-use it and format it the way you want it when you want it. Or you store a string version of it and you'll have to re-do the conversion plus formatting later.



  • Strangely this works
    "participant_time VARCHAR(20)"

    But should't this be working for the TIMESTAMP data-type too?.


  • Lifetime Qt Champion

    What does work ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.