PDA

View Full Version : Simplest SQL example



janus66
9th October 2009, 14:37
I want to do this:
1. connect to an odbc database connection
2. execute a query.
3. exit my program

Can anyone please show me how to do this simple task in QT?:crying:

lasher
9th October 2009, 18:55
Hi

Please check sqlbrowser example, it's quite simple

Lykurg
9th October 2009, 19:39
this is not a difficult thing. just c&p from the docs:


#include <QtGui>
#include <QtSql>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
bool ok = db.open();

if(ok)
{
QSqlQuery query("SELECT country FROM artist");
while (query.next())
{
QString country = query.value(0).toString();
qWarning() << country;
}
}

return app.exec();
}



By reading the docs about QSqlDatabase you should be able to connect via ODBC.

waniujjwal
21st July 2011, 07:40
Hi,

I want to use qt sql component from plain c++ from visual studio.
Can anyone please provide me sample for this?

I could learn about qt's add-in for visual studio but as per my understanding that becomes managed code (This dependency on .net framework i don't want to add).

I just want to use library (qt-sql) in my c++ project so i can access database from my application.

Thanks in advance.

Regards,
Ujjwal