Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT and MySQL

QT and MySQL

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Riyal
    wrote on last edited by
    #1

    I've done alot of thorought searching already but I can't find proper or maybe clear info for this so I decided to ask here myself. This is just a short one so please answer... Thanks!

    I'm still newbie on developing QT apps so just wanna ask how could I do the following in QT?

    1. mysql_fetch_array()
    2. mysql_fetch_row()
    3. mysql_num_fields()
    4. mysql_num_rows()
      Again i'm a total newbie at this and still just learning.

    a basic example would be better again thanks in advance! ^_^

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Riyal
      wrote on last edited by
      #2

      oh yeah... so far this is the most I can do to retrieve specific data over the database...
      @ QSqlTableModel model;
      model.setTable("test1");
      model.setFilter("");
      model.select();

      for (int i = 0; i < model.rowCount(); ++i) {
          QSqlRecord record = model.record(i);
          QString uname = record.value("username").toString();
          QString email = record.value("email").toString();
      
          if(uname == "qwesdsd") {
          QLabel *poplabel = new QLabel();
          poplabel->setText("Email: "+email);
          poplabel->show();
      }@
      
      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dii
        wrote on last edited by
        #3

        I'm not 100% sure, what do you want, but for the PHP comand above, you should look for the very simple QSqlQuery. The QSqlTableModel is more for a model/view implementation, that probably you won't need.

        You can set your query with QSqlQuery easy, and you have similar functions as you need, take a look at seek(), size(), value(..) and record(). The documentation is rather good on this:

        "http://doc.qt.nokia.com/4.7/qsqlquery.html":http://doc.qt.nokia.com/4.7/qsqlquery.html

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stukdev
          wrote on last edited by
          #4

          example?
          There are only for a examples, no control, no efficent, is only for understand how can you set a simple query...

          //Open mysqlite db
          @void sql::connect()
          {
          bool ok;

          //Connect db
          db = QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName("test.sqlite");
          ok = db.open();
          if(!ok)
          {
              qDebug() << db.lastError();
              exit(1);
          }
          

          }
          @

          //Create a table and insert some value
          @void sql::test()
          {
          QSqlQuery query;
          QString str = QString("CREATE TABLE t(x INTEGER, y, z, t,PRIMARY KEY(x ASC))");
          query.prepare(str);
          query.exec();

          str = QString("INSERT INTO t VALUES(10, 5, 7, 'test'&#41;"&#41;;
          query.prepare(str);
          query.exec&#40;&#41;;
          

          }@

          //Simple get some values separate from a ; in a QStringList

          @QStringList sql::getActionUser(qint32 iduser)
          {
          QSqlQuery query;
          QString str = QString("SELECT A.id,A.name,A.data FROM Action AS A WHERE A.iduser= '%1' ORDER BY A.data DESC").arg(iduser);
          QStringList result;

          result.clear();
          
          query.setForwardOnly(1);
          query.prepare(str);
          
          if(query.exec&#40;&#41;&#41;
          {
              while(query.next(&#41;)
              {
                  if(query.isValid())
                      result <<  query.value(0).toString() + ";" + query.value(1).toString() + ";" + query.value(2).toString();
              }
          }
          
          query.clear();
          return result;
          

          }@

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved