I need some help in updating remote MySQL database using QT5.I am working on Linux Ubuntu 12.04.I want my app to connect to remote server and write some data to it.Any help will be greatly appreciated.
Asked
Active
Viewed 577 times
1 Answers
0
Here is an example:
#include <QCoreApplication>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//use mysql driver
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//set hostname
db.setHostName("localhost");
//set db name
db.setDatabaseName("test");
//set username and password
db.setUserName("user");
db.setPassword("pass");
//open db
bool ok = db.open();
qDebug() << "Db is open: " << ok;
//define a query
QSqlQuery query;
//set query
query.exec("SELECT * FROM `Persons`");
//get values from query
while (query.next()) {
QString LastName = query.value(1).toString();
QString FirstName = query.value(2).toString();
int age = query.value(3).toInt();
qDebug() << LastName << " " << FirstName << " " << age;
}
//close db
db.close();
return a.exec();
}

demosthenes
- 1,151
- 1
- 13
- 17
-
create a database with name "test" and a table"Persons" CREATE TABLE Persons ( ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID) ); – demosthenes Nov 14 '17 at 19:46