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. [SOLVED] How can I replace record in my QSqlDatabase ?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How can I replace record in my QSqlDatabase ?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 3.1k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    I'm a newbie in db programming. So I need some answer. I wrote some code :
    @QList<QSqlRecord> records = GET_SCO_DB()->getRecords(tableName, keyField, keyValue);

    if (records.count() <= 0)
    return false;

    QSqlRecord record = records.at(0);

    for (int i = 0; i < record.count(); i++) {

    QSqlField field = record.field(i);
    field.setValue(dbProps.value(field.name()));

    record.replace(i, field);
    }@
    When @GET_SCO_DB@ is single-tone class instance and it's method getRecords returns me list of record by given parameters: @QSqlQuery query(*this);

    QString sql;

    if (keyValue.isNull()) {

    sql = QString("SELECT * FROM %1 WHERE %2 IS NULL")
    .arg(table)
    .arg(keyField);

    query.prepare(sql);

    } else {
    sql = QString("SELECT * FROM %1 WHERE %2 = ?")
    .arg(table)
    .arg(keyField);

    query.prepare(sql);
    query.addBindValue(keyValue);
    }

    if (false == query.exec()) {
    qDebug() << query.lastError().text();
    return QList<QSqlRecord>();
    }

    QList<QSqlRecord> descriptions;

    while (query.next()) {
    QSqlRecord record = query.record();
    descriptions.append(record);
    }

    return descriptions;@
    So how can I make to my code being executed to db ? As you see from first code that I post i get QSqlRecord and replaced some fields there. Now I need to apply changes to db.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Code_ReaQtor
      wrote on last edited by
      #2

      Use "UPDATE":http://www.w3schools.com/sql/sql_update.asp statement.

      Please visit my open-source projects at https://github.com/Code-ReaQtor.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anticross
        wrote on last edited by
        #3

        I write function like this:
        @bool SCODatabase::updateRecord(const QString & table, const QString & keyField, const QVariant & keyValue, const sxProperties & newValues) {

        QSqlQuery query(*this);
        QString sql = QString("UPDATE %1 SET ").arg(table);

        QHashIterator<QString, QVariant> iter(newValues);

        int cnt = newValues.count();

        while(iter.hasNext()) {

        iter.next();
        cnt--;

        QString sValue = iter.value().toString();

        if(sValue.isEmpty())
        sValue = "NULL";

        if (cnt <= 0)
        sql += QString("%1=%2").arg(iter.key()).arg(sValue);
        else
        sql += QString("%1=%2, ").arg(iter.key()).arg(sValue);
        }

        sql += QString(" WHERE %1 = ?").arg(keyField);

        query.prepare(sql);
        query.addBindValue(keyValue);

        if (false == query.exec(sql)) {
        qDebug() << query.lastError().text();
        return false;
        }

        return true;
        }@
        but lastError returns me this: "ERROR: syntax error at end of input
        LINE 1: ... SET ZONE_TAREGT_OUT=0, ZONE_TAREGT_IN=0 WHERE ZONE_NAME = ?
        ^
        QPSQL: Unable to create query"

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          [quote author="Anticross" date="1358764224"]

          but lastError returns me this: "ERROR: syntax error at end of input
          LINE 1: ... SET ZONE_TAREGT_OUT=0, ZONE_TAREGT_IN=0 WHERE ZONE_NAME = ?
          ^
          QPSQL: Unable to create query" [/quote]

          There might be a problem in your sql syntax:
          @SET ZONE_TAREGT_OUT=0, ZONE_TAREGT_IN=0 WHERE ZONE_NAME = ?^@

          what is "?^" anyways?

          you might want to take a look a psql "specifics":http://www.ntchosting.com/postgresql/#Database_triggers

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anticross
            wrote on last edited by
            #5

            Damn, I just need to add ' characters. @bool SCODatabase::updateRecord(const QString & table, const QString & keyField, const QVariant & keyValue, const sxProperties & newValues) {

            QSqlQuery query(*this);
            QString sql = QString("UPDATE %1 SET ").arg(table);

            QHashIterator<QString, QVariant> iter(newValues);

            int cnt = newValues.count();

            while(iter.hasNext()) {

            iter.next();
            cnt--;

            QString sValue = iter.value().toString();

            if(sValue.isEmpty())
            sValue = "NULL";

            if (cnt <= 0)
            sql += QString("%1='%2'").arg(iter.key()).arg(sValue);
            else
            sql += QString("%1='%2', ").arg(iter.key()).arg(sValue);
            }

            sql += QString(" WHERE %1='%2'").arg(keyField).arg(keyValue.toString());

            if (false == query.exec(sql)) {
            qDebug() << query.lastError().text();
            return false;
            }

            return true;
            }@

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #6

              [quote author="Anticross" date="1358769615"]Damn, I just need to add ' characters. [/quote]

              Actually, that is a MUST for datatypes like texts/strings. Happy coding!

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anticross
                wrote on last edited by
                #7

                And last update with NULL values handling: @bool SCODatabase::updateRecord(const QString & table, const QString & keyField, const QVariant & keyValue, const sxProperties & newValues) {

                QSqlQuery query(*this);
                QString sql = QString("UPDATE %1 SET ").arg(table);

                QHashIterator<QString, QVariant> iter(newValues);

                int cnt = newValues.count();

                while(iter.hasNext()) {

                iter.next();
                cnt--;

                QString sValue = iter.value().toString();

                if(sValue.isEmpty())
                sql += QString("%1=NULL").arg(iter.key());
                else
                sql += QString("%1='%2'").arg(iter.key()).arg(sValue);

                if (cnt > 0)
                sql += QString(", ");
                }

                sql += QString(" WHERE %1='%2'").arg(keyField).arg(keyValue.toString());

                if (false == query.exec(sql)) {
                qDebug() << query.lastError().text();
                return false;
                }

                return true;
                }@ Thanks, Code_ReaQtor, for help.

                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