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. Qt QSqlRecord and mySQL Functions [SOLVED]

Qt QSqlRecord and mySQL Functions [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k Views
  • 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.
  • T Offline
    T Offline
    TrusT1234
    wrote on 1 Mar 2015, 17:21 last edited by
    #1

    Hello,

    I have a question about QSqlRecord and mySQL Function. I wrote a function which inserts new values to a database and it is necessary for the further steps to receive the last inserted ID. The mySQL function works fine and I receive the ID if I call the function directly on the command line, but if I call the function through my Qt console application a get always 0. I tried to use the QSqlQuery method lastInsertId() but I get invalid so it seems that my database do not support this method.
    I hope you can help me and you understant the problem.

    Source code for mySQL function:
    @CREATE FUNCTION func_insertTest (
    test1 VARCHAR(100),
    test2 VARCHAR(10),
    test3 VARCHAR(20),
    next_id INT
    )
    RETURNS INT
    BEGIN
    INSERT INTO tbl_mvTest
    VALUES (NULL, test1, test2, test3, next_id);

    RETURN LAST_INSERT_ID();
    END;@

    Qt source code:
    @query.prepare("SELECT func_insertTest( "Test", "Test", "Test", NULL) as result;");
    query.exec();
    QSqlRecord record = query.record();
    QVariant test = query.value(0);@

    Thank you!

    Best regards,
    TrusT

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Mar 2015, 07:37 last edited by
      #2

      I have never tried a function but,

      you can try
      @
      while ( query.next() ) {
      QVariant test = query.value(0);
      qDebug() << test;
      }@

      Since you need to place "query" on a valid record.

      also try

      @
      qDebug() << query.lastError();
      @

      To see if it fails calling your function.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TrusT1234
        wrote on 2 Mar 2015, 10:11 last edited by
        #3

        I tried your idea and I got an error message:
        @QMYSQL3: Unable to fetch data@

        Now I understand why I cannot get the required information but I have not realy an idea to solve this problem...

        I added this source code:
        @while(query.next()) {
        QVariant test = query.value(0);
        QString inerror = query.lastError().text();
        }
        QString outerror = query.lastError().text();@

        The application does not go inside the loop so the error message is from the outerror variable.

        at mrjj: Thank you for your help!

        I tried to get the fieldname without a loop and I can get it. The following code reads the fieldname correctly but if I use this name for reading the value I receive always 0 but it should be another value...
        @query.prepare("SELECT func_insertTest( "Test", "Test", "Test", NULL) as result;");
        query.exec();
        QSqlRecord record = query.record();
        QString fieldname = record.fieldName(0);
        QVariant data = record.value(fieldname);
        QString outerror = query.lastError().text();@

        So why can I get the fieldname but not the value?
        Thank you!

        Best regards,
        TrusT

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TrusT1234
          wrote on 2 Mar 2015, 16:03 last edited by
          #4

          Hello,

          I could solve the problem! Actually it was a very simple problem... It seems that the mySQL driver was too old I changed it to the newest version and it works!
          Thank you mrjj for your help!!

          Best regards,
          TrusT

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 3 Mar 2015, 07:27 last edited by
            #5

            Hello, that is good to hear.

            Might help others trying out SQL with QT.

            Thank you for posting the solution.

            Please press Edit under your picture on first post and change
            title to [SOLVED] if possible.

            Best Regards

            1 Reply Last reply
            0

            1/5

            1 Mar 2015, 17:21

            • Login

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