Qt QSqlRecord and mySQL Functions [SOLVED]



  • 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


  • Qt Champions 2016

    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.



  • 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



  • 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


  • Qt Champions 2016

    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


Log in to reply
 

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