PDA

View Full Version : Linking Qt with Database MySql



SumitVaise
3rd August 2015, 05:59
i am trying to link QT with DB its saying its connected and opened but when am trying to fetch information of a table its doing nothing.

#include<QtCore/QCoreApplication>
#include<QtSql>
#include<QtDebug>
int main(int argc, char *argv[])
{
QString servername="MyFirstConn";
QString dbname ="Dataset";
QSqlDatabase db= QSqlDatabase :: addDatabase("QSQLITE");
db.setConnectOptions();

QString dsn = QString ("Driver={Sql Native Client}; SERVER =%1; DATABASE =%2; Trusted_Connection=Yes;").arg(servername).arg(dbname);

db.setDatabaseName(dsn);
if(db.open())
{

qDebug ()<<"opened!";
QSqlQuery qry;
if (qry.exec("Select * from Dataset.ImageDataset!!!"))
{
while(qry.next())
{
qDebug()<<qry.value(2).toString();

}

}
else
{
qDebug ()<<"Error= "<< db.lastError().text();
}
qDebug ()<<"Closed!";
db.close();
}
else
{
qDebug ()<<"Error="<< db.lastError().text();
}
}
Kindly help.
Thanks

jefftee
3rd August 2015, 06:14
Are you trying to use SQLITE or MySQL?

ChrisW67
3rd August 2015, 21:13
... Or ODBC to Microsoft Sql Server or something else? This is one of the best examples of cut 'n' paste "efficiency" I have seen for a while.

Generic how to connect
http://doc.qt.io/qt-4.8/qsqldatabase.html#details
http://doc.qt.io/qt-4.8/sql-driver.html#supported-databases

There is even a Mysql example that fairly easily found
http://doc.qt.io/qt-4.8/sql-connecting.html