Results 1 to 4 of 4

Thread: How to send a mysql script from QT application?

  1. #1
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to send a mysql script from QT application?

    I want to execute the following SQL code using a QT application (entered from line edit).
    DROP PROCEDURE IF EXISTS mprocedure;
    DELIMITER $$
    CREATE PROCEDURE mprocedure ()
    BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
    ROLLBACK;
    SELECT 'An exception occurred';
    END;
    START TRANSACTION;
    INSERT INTO `mschema`.`table1`
    (`maxbudget`, `blocked`, `d_percentage`, `max discount`)
    VALUES ('2250', '0', '.9', '.99');
    INSERT INTO `mschema`.`table2`
    (`name`,`image`,`date`,`fKey_id`)
    VALUES ('jhon','jfdd', '2018-01-01 00:00:00', LAST_INSERT_ID());
    COMMIT;
    END;
    $$
    DELIMITER ;

    When i try to execute it, the exec() function does not return error but the database is not affected. When i copy this query and past in MySQL workbench, it is executed successfully. When i try to execute call mprocedure(); from qt application, it is executed successfully and made the required effect on my database

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send a mysql script from QT application?

    Show real code.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,670
    Thanks
    13
    Thanked 1,595 Times in 1,523 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to send a mysql script from QT application?

    There are two issues I can see:
    1. QSqlQuery::exec() executes a single SQL statement and you are trying to pass two (DROP and CREATE PROCEDURE BEGIN ... END)
    2. The DELIMITER lines are not SQL, they are directives to the mysql commandline tool to ensure it passes the whole procedure in one block rather than stopping at the first semicolon. The Mysql command line tool is not involved here.

    You should be able to execute the DROP in one exec() call, and the CREATE PROCEDURE (with the internal semicolons as-is) in a second. If you are trying to handle a user-provided script with Mysql command line tool syntax then:
    • you need to write a simple parser to split the script into statement and send them one at at time
    • you need to really trust your user not to bork your database

  4. The following user says thank you to ChrisW67 for this useful post:

    Ahmed Abdellatif (4th May 2018)

  5. #4
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to send a mysql script from QT application?

    I know it is too late, but thanks a lot.
    smallcorrection:
    into statement should be into 2 statements
    Last edited by Ahmed Abdellatif; 4th May 2018 at 17:53.

Similar Threads

  1. How C application send notification to Qt application on Linux
    By Rajesh.Rathod in forum General Programming
    Replies: 3
    Last Post: 28th February 2018, 14:13
  2. Send qt gui application in background..
    By ajaysaini.sgvu in forum Qt Programming
    Replies: 3
    Last Post: 18th August 2012, 09:19
  3. Replies: 3
    Last Post: 29th October 2011, 00:24
  4. send an email from my Qt application
    By omega36 in forum Qt Programming
    Replies: 14
    Last Post: 22nd December 2008, 18:06
  5. Replies: 3
    Last Post: 6th February 2006, 18:41

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.