Important: Please read the Qt Code of Conduct -

Accessing Online Database From Qt

  • Hi everybody,
    I'd like to know if the following code:

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        bool ok =;

    Is this able to connect to a database located to a server? Or just to connect to a local database (Offline Database)?

    Thanks a lot!

  • Hello,

    That should be enough, as long as the host name is the correct name or IP address of the server and there is no firewall blocking the connection. You may also need to provide the port with db.setPort(int)(the default for MySQL is 3306).

  • Lifetime Qt Champion


    As noted by @panosk, it's enough to connect to a local or remote instance of a MySQL database.

    However, if you plan to connect to a remote server outside of your network, you should reconsider your design. A database should never be accessible directly over internet.

  • Hi, thank you for your answers,
    I'm trying to connect to a MySQL located in my Hostinger "account".
    Do you know if it has firewall? Because I'm unable to connect.


  • Moderators

    There's something more beside the firewalls. Some hosting companies will disable (it's a MySQL server feature) logging into databases from hosts different than the local one, and for a good reason. You should check that as well, even if the port is open and the server is visible you could still fail to connect if that's the case.

  • You probably want to try to connect to your database using an existing tool before you try it with your own software. This way you are at least sure that the problem is in fact in your software if it doesn't work and not somewhere else (eg. host configuration as mentioned by @kshegunov)

Log in to reply