QT and MySQL



  • 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! ^_^



  • 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();
    }@


  • 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



  • 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;
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.