Is it possible to connect the program on qt to Mysql database on another server and change this database?

  • one
    Here is a look. - Deft
  • Yes, you can ... the connection string to MySql looks like this: Database=DATABASENAME;Server=SERVERNAME;UID=USER;PWD=PASS; - Dmitry Chistik

2 answers 2

The IP address of the database server is set using the method

 void QSqlDatabase::setHostName(const QString& hostName); 

and it does not matter whether it is a local host or a remote one.

Study the documentation .

The simplest example of using Qt tools to work with the database:

 QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("your.remote.db.host"); db.setDatabaseName("your_db_name"); db.setUserName("your_username"); db.setPassword("your_password"); if (db.open()) { QSqlQuery query(db); if (query.exec("SELECT * FROM your_table;")) { while (query.next()) { QSqlRecord row = query.record(); for (int i = 0; i < row.count(); ++i) { qDebug() << QString("%1 = %2") .arg(row.fieldName(i)) .arg(row.value(i).toString()); } } } } 

In the same way, you can perform INSERT / UPDATE / DELETE queries. The main thing is that the database server is available, and your user would have enough rights to change the database.

  • one
    Do not forget to allow remote connection on the server. - maestro

Yes. Personally, it turned, everything worked.

You will need QSqlDatabase first.

  • ATP Will Dig - Madoka Magica