Results 1 to 3 of 3

Thread: Linking Qt with Database MySql

  1. #1
    Join Date
    Aug 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Linking Qt with Database MySql

    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(dbna me);

    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

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Linking Qt with Database MySql

    Are you trying to use SQLITE or MySQL?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Linking Qt with Database MySql

    ... 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.h...rted-databases

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

Similar Threads

  1. How to Connect to MySQL database
    By johnL in forum Newbie
    Replies: 12
    Last Post: 16th May 2015, 16:43
  2. Qt and MySQL database
    By Stanfillirenfro in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2013, 07:58
  3. Cannot connect to mysql database
    By phapha in forum Newbie
    Replies: 3
    Last Post: 27th October 2011, 14:58
  4. Linking Sql Database in QtQuick
    By chronoski in forum Newbie
    Replies: 1
    Last Post: 17th March 2011, 03:46
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.