Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQL Data type error
Forum Updated to NodeBB v4.3 + New Features

SQL Data type error

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MuzabM Offline
    MuzabM Offline
    Muzab
    wrote on last edited by
    #1

    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!)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • MuzabM Offline
        MuzabM Offline
        Muzab
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • MuzabM Offline
            MuzabM Offline
            Muzab
            wrote on last edited by
            #5

            I store the HEX in the table as follows:

             "participant_tag  VARCHAR(20)"
             "participant_time  TIMESTAMP"
            
            1 Reply Last reply
            0
            • MuzabM Offline
              MuzabM Offline
              Muzab
              wrote on last edited by
              #6

              @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 ?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • MuzabM Offline
                  MuzabM Offline
                  Muzab
                  wrote on last edited by
                  #8

                  Strangely this works
                  "participant_time VARCHAR(20)"

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What does work ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved