I am new to Qt and I'm trying to connect a remote database.
QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL" );
db.setHostName("IP ADRESS"); // local or IP
db.setPort(3306); // Default port is 3306
db.setUserName("DB USER"); // example root
db.setPassword("PASSWORD");
db.setDatabaseName("DB NAME");
if( !db.open() )
{
qDebug() << db.lastError();
qFatal( "Failed to connect." );
}
qDebug( "Connected!" );
QSqlQuery qry;
qry.prepare( "CREATE TABLE IF NOT EXISTS data (id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30))" );
if( !qry.exec() )
qDebug() << qry.lastError();
else
qDebug() << "Table created!";
... goes on
this code works like a charm in local but when i give my server ip instead of localhost i can not connect. Should i use a different mechanism?
PS:I've never retrieved data from web before.