Results 1 to 14 of 14

Thread: Moving Script out of main.cpp to own .cpp file (MySQL)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Moving Script out of main.cpp to own .cpp file (MySQL)

    Hello guys,

    im new in c++...
    so I have come up with own function which I have placed into blank mysql.cpp (please note, the code bellow is ALL mysql.cpp content, there are no #includes or anything else:
    Qt Code:
    1. void sql_insertValue(QString user, Qstring terminal, Qstring taskKind, Qstring taskType, Qstring timestamp)
    2. {
    3. QBool result;
    4.  
    5. // Sets database
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    7. db.setHostName("localhost");,
    8. db.setDatabaseName("terminals");
    9. db.setUserName("root");
    10. db.setPassword("");
    11. if(!db.open()) { qDebug() << "MySQL ERROR: " << db.lastError().text(); }
    12.  
    13. QSqlQuery qry;
    14. qry.prepare( "INSERT INTO records (user, kst, taskkind, taktype, timestamp) VALUES ('%1', '%2', '%3', '%4', '%5')" ).argv(user).argv(terminal).argv(taskKind).argv(taskType).argv(timestamp);
    15. if( !qry.exec() )
    16. result = false;
    17. else
    18. result = true;
    19.  
    20. db.close();
    21.  
    22. return result;
    23. }
    To copy to clipboard, switch view to plain text mode 

    so I dont know how to make this work when called from QML:
    - meaning how to include this file from main.cpp
    - how to register it as public function so i can call it from QML
    - how to read result from QML: if(sql_insertValue(a,b,c,d)===true) { something }

    If I can ask you to guide me as total noob, please do so as this is my (nearly) totaly first interact with c++ inside qt.

    so I know main.cpp must mo modified, mysql.cpp must be modified (at least some includes), and also some import in qml should be done... probably something else...

    Thank you
    Last edited by shokarta; 31st March 2019 at 18:09.

Similar Threads

  1. How to send a mysql script from QT application?
    By Ahmed Abdellatif in forum Newbie
    Replies: 3
    Last Post: 4th May 2018, 16:26
  2. Replies: 4
    Last Post: 26th September 2017, 07:49
  3. Replies: 4
    Last Post: 8th March 2016, 14:27
  4. Replies: 1
    Last Post: 28th January 2012, 13:59
  5. How to get value from Script file?
    By superinman in forum Qt Programming
    Replies: 2
    Last Post: 30th July 2010, 23:06

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.