Results 1 to 14 of 14

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

  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 19:09.

  2. #2
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    I have got this far (with check connection first only):

    mysql.h:
    Qt Code:
    1. #ifndef MYSQL_H
    2. #define MYSQL_H
    3.  
    4. #include <QObject>
    5.  
    6. class MySQL : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit MySQL(QObject *parent = 0);
    11. Q_INVOKABLE void checkConnection();
    12.  
    13. signals:
    14.  
    15. public slots:
    16.  
    17. };
    18.  
    19. #endif // MYSQL_H
    To copy to clipboard, switch view to plain text mode 

    mysql.cpp:
    Qt Code:
    1. #include "mysql.h"
    2. #include <QtSql>
    3. #include <QObject>
    4.  
    5. MySQL::MySQL(QObject *parent) :
    6. QObject(parent)
    7. {
    8. }
    9.  
    10. void MySQL::checkConnection()
    11. {
    12. QString result;
    13. // Sets database
    14. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    15. db.setHostName("localhost");
    16. db.setDatabaseName("terminals");
    17. db.setUserName("root");
    18. db.setPassword("");
    19.  
    20. if(db.open()) { qDebug() << "MySQL: Connected"; db.close(); result = "true"; }
    21. else { qDebug() << "MySQL ERROR: " << db.lastError().text(); result = "false"; }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    main.qml:
    Qt Code:
    1. import QtQuick 2.12
    2. import QtQuick.Controls 2.0
    3. import QtQuick.LocalStorage 2.0
    4. import MySQL 1.0
    5. import 'JavaScript.js' as JS
    6.  
    7. Item {
    8.  
    9. MySQL {
    10. id: sql
    11. }
    12.  
    13. MouseArea {
    14. anchors.fill: parent
    15. onClicked: { sql.checkConnection(); }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtCore>
    2. #include <QNetworkAccessManager>
    3. #include <QNetworkReply>
    4. #include <QGuiApplication>
    5. #include <QQmlApplicationEngine>
    6. #include <QtSql>
    7. #include <QtDebug>
    8. #include "mysql.h"
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QGuiApplication app(argc, argv);
    13.  
    14. qmlRegisterType<MySQL>("MySQL", 1, 0, "MySQL");
    15.  
    16. QQmlApplicationEngine engine;
    17. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    18.  
    19. // Sets path for SQLite
    20. engine.setOfflineStoragePath("C:\\Terminal");
    21.  
    22. return app.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    ¨

    this works like a charm!

    i would like to ad the return ability, so from QML i can do something like:
    if(sql.checkConnection()===true) { something } else { somethingelse }
    - so it will return result variable from mysql.cpp

    how to do this?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    You simply change the return type of your function from "void" to the type you want to return, e.g. "bool" for a true/false value.

    Then you can use one or more "return" lines inside the function.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    wooow that was real simple

    this brings additional 3 questions:
    1) how do I move rows 13 to 18 in mysql.cpp above the void (now its bool), because I wish to have some more functions but the data for login to DB stays the same...
    I have tried to move them just above the void (bool) function, but error uncnown type name 'db' and if i moved it inside MySQL::MySQL(QObject *parent): then I am getting error use of undeclared identifier 'db'

    2) as soon as i request this function via MouseArea onclick, the whole application freezes for 3 sec till it return false as db unreachable (in case db is reachable, its just few milsec so its unnoticable). Is there anything what I can do? run the script on background or something?

    3) can I somehow make a signal if connection status changes? so far I have timer that every 30 sec it will update and check if connection is available, but In case the status will changhe, is it possible that it will notify me myself like OnVariableChanges or something?

    Thank you

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by shokarta View Post
    this brings additional 3 questions:
    1) how do I move rows 13 to 18 in mysql.cpp above the void (now its bool), because I wish to have some more functions but the data for login to DB stays the same...
    I have tried to move them just above the void (bool) function, but error uncnown type name 'db' and if i moved it inside MySQL::MySQL(QObject *parent): then I am getting error use of undeclared identifier 'db'
    "db" is currently a local variable.
    If you move the block that defines that variable, then it is only valid in that new location.

    What you want is a member variable, a variable that is valid in all methods of an object
    Qt Code:
    1. class MySQL : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. // ...
    6.  
    7.  
    8. private:
    9. QSqlDataBase db;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Some very common coding convention prefix member variables with "m_" do make them clearly visible as such in the code
    Qt Code:
    1. private:
    2. QSqlDataBase m_db;
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by shokarta View Post
    2) as soon as i request this function via MouseArea onclick, the whole application freezes for 3 sec till it return false as db unreachable (in case db is reachable, its just few milsec so its unnoticable). Is there anything what I can do? run the script on background or something?
    Possible with multithreading, but then it gets much more complicated as everything becomes asynchronous.

    Quote Originally Posted by shokarta View Post
    3) can I somehow make a signal if connection status changes? so far I have timer that every 30 sec it will update and check if connection is available, but In case the status will changhe, is it possible that it will notify me myself like OnVariableChanges or something?
    Not with the QSql API.
    It might be possible to do that by polling, i.e. trying to open/close a test connection in regular intervals, or via the MySQL C-API.

    Cheers,
    _

  6. #6
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    For the point one, thank you, works beautyfuly!

    For second point, can you provide any example? Not that it would be usefull here, but I also have on QML an icon indicator weather DB is online or not, this also freezes up the whole app if connection is not online, in here the multithreading would be awesome solution. Also is there somehow possible too give the whole app some Picture of loading while its freezed up because of this?

    And for the third point, can you please provide an ylink for how its done in C API? I tried google it up, but no luck.

    Thank you

  7. #7
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by shokarta View Post
    For second point, can you provide any example? Not that it would be usefull here, but I also have on QML an icon indicator weather DB is online or not, this also freezes up the whole app if connection is not online, in here the multithreading would be awesome solution. Also is there somehow possible too give the whole app some Picture of loading while its freezed up because of this?
    I did figure out FastBlur + some PopUp with image... i set the raidus to something whenever its processing anythign with db, and back to 0 when its done...
    This is good and usable... however the indicator of if db is online or not should be done on background as multithread, please share some link for this if you can

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Multithreading is quite complicated, a lot of possible errors if not handled correctly and very difficult to debug.

    https://doc.qt.io/qt-5/examples-thre...oncurrent.html

    How do the other programs that access the same database handle the case of it not being reachable?

    Cheers,
    _

  9. #9
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by anda_skoa View Post
    How do the other programs that access the same database handle the case of it not being reachable?
    I dont understand the question, you mean this?:
    Qt Code:
    1. MySQL::MySQL(QObject *parent) :
    2. QObject(parent)
    3. {
    4. // Sets Termianl Database
    5. terminal_db = QSqlDatabase::addDatabase("QMYSQL", "ExternalDB");
    6. terminal_db.setHostName("localhost");
    7. terminal_db.setDatabaseName("terminals");
    8. terminal_db.setUserName("root");
    9. terminal_db.setPassword("");
    10. }
    11.  
    12. bool MySQL::checkConnection()
    13. {
    14. if(terminal_db.open()) { terminal_db.close(); return true; }
    15. else { qDebug() << "Terminal DB Error: " << terminal_db.lastError().text(); return false; }
    16. }
    To copy to clipboard, switch view to plain text mode 

    and the debug is:
    Qt Code:
    1. Terminal DB Error: "Can't connect to MySQL server on 'localhost' (10061) QMYSQL: Nepoda?ilo se navázat spojení"
    To copy to clipboard, switch view to plain text mode 
    translation would be "Coulnd not establish the connection"

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by shokarta View Post
    I dont understand the question, you mean this?:
    What I meant is this: you are running a database server and you likely do that because you have several programs connecting to it.
    How do these other programs deal with the database server not running?

    And which part of the system is responsible for starting the database server?
    Why has it failed to do so when you are getting the connection error?

    Cheers,
    _

  11. #11
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    OK, so this info is not realy relevant because:

    the server is runing MySQL server as the main application if Attendence database (you swift you RFID user card and its recorded to db where its stored with your personal details - as every company has).

    But I am developing a program on WinMate terminals, where you also swap your card, it reads its info and connect to this attendence DB and gets your personal info from the card ID, then save the record when you swifted to different mysql db...

    therefore I need to check if I have available connection to db so I can save my record to outside mysql db, or to internal sqlite (and later save the records from sqlite to mysql)...

    So, after all how other programs handle this is not relevant and available...

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Quote Originally Posted by shokarta View Post
    therefore I need to check if I have available connection to db so I can save my record to outside mysql db, or to internal sqlite (and later save the records from sqlite to mysql)...
    You say about "outside database" : is localhost correct address ?

  13. #13
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    for the testing yes, once the application is done, i will change the login data to the database to outside source...

  14. #14
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Can You connect to database with this parameters and standard MySQL client ?

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, 17:26
  2. Replies: 4
    Last Post: 26th September 2017, 08:49
  3. Replies: 4
    Last Post: 8th March 2016, 15:27
  4. Replies: 1
    Last Post: 28th January 2012, 14:59
  5. How to get value from Script file?
    By superinman in forum Qt Programming
    Replies: 2
    Last Post: 31st July 2010, 00: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.