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] function with a defaul QVector parameter value
QtWS25 Last Chance

[SOLVED] function with a defaul QVector parameter value

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

    basically I have my PHP mysql class that use pdo that is @public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC) {
    $sth = $this->prepare($sql);
    foreach ($array as $key => $value) {
    $sth->bindValue("$key", $value);
    }
    $sth->execute();
    return $sth->fetchAll($fetchMode);
    }@

    but I can not make it to run under qt because I can not initialize the myArray to nil or null or empty
    Qt code
    @QSqlQuery ATP_db::atpSelect(QString sqlQuerry, QVector<QString> *myArray = new myArray ) {
    QSqlQuery myQuerry;
    myQuerry.prepare(sqlQuerry);
    QDebug() << myQuerry;
    foreach (QVector<QString> myQuerryV, myArray) {
    QDebug() << myQuerryV;
    }
    // myQuerry.exec(sqlQuerry);
    return myQuerry;
    }@
    please help if you have some time...
    Thanks for your time...
    and for some.. have a good evening.. :P

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      If you need to pass a default value, then you can do it in the function declaration:

      • ATP_db.h
        @
        class ATP_db
        {
        ...
        public:
        ...
        QSqlQuery atpSelect(QString sqlQuerry, QVector<QString> *myArray = nullptr);
        ...
        };
        @

      • ATP_db.cpp
        @
        QSqlQuery ATP_db::atpSelect(QString sqlQuerry, QVector<QString> *myArray)
        {
        ...
        }
        @

      Side note: It's better to pass a reference to a container instead of a pointer, because with a NULL pointer you will have a crash at "foreach" line.

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

        if not ok because of the following errors
        D:\atp-trading\qt-electricity\electricity\atp_db.h:18: warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]
        QSqlQuery atpSelect(QString sqlQuerry, QList<QString> *myArray = nullptr);
        D:\atp-trading\qt-electricity\electricity\atp_db.h:18: error: 'nullptr' was not declared in this scope
        QSqlQuery atpSelect(QString sqlQuerry, QList<QString> *myArray = nullptr);

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          Right, my bad, I assumed that everybody is using C++ 11 nowadays :-)
          nullptr is c++11 feature.
          If you don't use c++11 then use NULL instead of nullptr.

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

            ok the working bit

            • ATP_db.h
              @#ifndef ATP_DB_H
              #define ATP_DB_H

            #include <QMap>
            #include <QVariant>
            #include <QSqlDatabase>

            class ATP_db
            {
            private:
            QSqlDatabase mydb;
            QString dbType;
            QString dbPath;

            public:
            bool atp_db();
            QSqlQuery atpSelect(QString sqlQuerry, QMap<QString, QVariant> *myArray = NULL>);
            QSqlQuery atpInsert(QString tableName, QMap<QString, QVariant> *data );
            QSqlQuery atpUpdate(QString tableName, QMap<QString, QVariant> *data, QString whereTo);
            QSqlQuery atpDelete(QString tableName, QString whereTo);
            };

            #endif // ATP_DB_H
            @

            • ATP_db.cpp
              @#include "atp_db.h"
              #include "atp_readini.h"
              #include <QtSql>
              #include <QFile>
              #include <QMessageBox>
              #include <QApplication>

            bool ATP_db::atp_db() {
            //reading ini file
            atp_readIni *readIni = new atp_readIni;
            dbPath = readIni->dbPath;
            dbType = readIni->dbType;
            //driver is available
            if(!QSqlDatabase::isDriverAvailable(dbType)){
            QMessageBox::critical(0, qApp->tr("Driver is not available"),
            qApp->tr("Unable to establish a database connection.\n"
            "This example needs SQLite support. Please read "
            "the Qt SQL driver documentation for information how "
            "to build it.\n\n"
            "Click Cancel to exit."), QMessageBox::Cancel);
            return false;
            }
            //new db for that type
            mydb = QSqlDatabase::addDatabase(dbType);
            QFile db_file(dbPath);
            if (!db_file.exists())
            {
            QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Database file not found. Create or import one!"), QMessageBox::Ok);
            atp_create_new_atpdb();
            //return false;
            }

            mydb.setDatabaseName(dbPath);
            if (!mydb.open()){
            qCritical() << "couldn't connect to database Error[" << mydb.lastError().text() << "]" << dbPath;
            return false;
            } else {
            qDebug() << "succsessfully connected to database " << dbPath;
            return true;
            }
            }

            QSqlQuery ATP_db::atpSelect(QString sqlQuerry, QMap<QString, QVariant> *myArray ) {
            QSqlQuery myQuerry;
            myQuerry.prepare(sqlQuerry);
            // qDebug() << myArray;
            if (myArray && !myArray->isEmpty()){
            QMap<QString, QVariant>::const_iterator i;
            for (i = myArray->constBegin(); i != myArray->constEnd(); ++i) {
            myQuerry.bindValue(i.key(), i.value());
            //myQuerry.bindValue(i.key().toUtf8().data(), i.value().toString().toUtf8().data());
            }
            }
            // qDebug() << myQuerry.boundValues();
            myQuerry.exec();
            // qDebug() << myQuerry.lastQuery() <<myQuerry.lastError();
            return myQuerry;
            }

            QSqlQuery ATP_db::atpInsert(QString tableName, QMap<QString, QVariant> data ) {
            QString fieldNames, fieldValues, temp;
            QMap<QString, QVariant>::const_iterator i1;
            for (i1 = data->constBegin(); i1 != data->constEnd(); ++i1) {
            temp = i1.key();
            fieldValues += temp + ", ";
            fieldNames += temp.remove(0,1) + ", ";
            }
            fieldNames.remove(fieldNames.lastIndexOf(","),1);
            fieldValues.remove(fieldValues.lastIndexOf(","),1);
            // qDebug() << fieldNames << "
            * - **" << fieldValues;

            QSqlQuery myQuerry;
            myQuerry.prepare("INSERT INTO " + tableName + " ( " + fieldNames + ") VALUES ( " + fieldValues + ")");
            // qDebug() << data;
            QMap<QString, QVariant>::const_iterator i;
            for (i = data->constBegin(); i != data->constEnd(); ++i) {
            myQuerry.bindValue(i.key(), i.value());
            //myQuerry.bindValue(i.key().toUtf8().data(), i.value().toString().toUtf8().data());
            }
            // qDebug() << myQuerry.boundValues();
            myQuerry.exec();
            // qDebug() << myQuerry.lastQuery() <<myQuerry.lastError();
            return myQuerry;
            }

            QSqlQuery ATP_db::atpUpdate(QString tableName, QMap<QString, QVariant> *data, QString whereTo) {
            QString fieldDetails, fieldNames, fieldValues, temp;
            QMap<QString, QVariant>::const_iterator i1;
            for (i1 = data->constBegin(); i1 != data->constEnd(); ++i1) {
            temp = i1.key();
            fieldValues = temp;
            fieldNames = temp.remove(0,1);
            fieldDetails += fieldNames + " = " + fieldValues + ", ";
            }
            fieldDetails.remove(fieldDetails.lastIndexOf(","),1);
            QSqlQuery myQuerry;
            myQuerry.prepare("UPDATE " + tableName + " SET " + fieldDetails + " WHERE " + whereTo);
            QMap<QString, QVariant>::const_iterator i;
            for (i = data->constBegin(); i != data->constEnd(); ++i) {
            myQuerry.bindValue(i.key(), i.value());
            }
            myQuerry.exec();
            // qDebug() << myQuerry.lastQuery() <<myQuerry.lastError();
            return myQuerry;
            }

            QSqlQuery ATP_db::atpDelete(QString tableName, QString whereTo){
            QSqlQuery myQuerry;
            myQuerry.prepare("DELETE FROM " + tableName + " WHERE " + whereTo);
            myQuerry.exec();
            // qDebug() << myQuerry.lastQuery() <<myQuerry.lastError();
            return myQuerry;
            }
            @

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              If you will call ATP_db::atpSelect() without second parameter then you will have a memory leak here, you allocate a memory for myArray but does not return it.
              If you always will call ATP_db::atpSelect() with second parameter then you don't need this default parameter.

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

                ok here you have an updated one - about ATP_db::atpSelect() second parameter the thing is unsure - sometimes we need it.. and also sometimes we do not need it

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andreyc
                  wrote on last edited by
                  #8

                  If a function is declared like this
                  @
                  QSqlQuery atpSelect(QString sqlQuerry, QMap<QString, QVariant> *myArray = new QMap<QString, QVariant>);
                  @
                  then application will leak memory on each call to atpSelect without second parameter.

                  If you don't need a second parameter then declare it as NULL and then check for NULL inside the function
                  @
                  atp.h
                  QSqlQuery atpSelect(QString sqlQuerry, QMap<QString, QVariant> *myArray = NULL);

                  atp.cpp
                  QSqlQuery ATP_db::atpSelect(QString sqlQuerry, QMap<QString, QVariant> *myArray)
                  {
                  ...
                  if (myArray && !myArray->isEmpty()){
                  ... do something with myArray
                  }
                  ...
                  return myQuerry;
                  }
                  @

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    arsinte_andrei
                    wrote on last edited by
                    #9

                    Thanks to andreyc we just have an updated source code...
                    Many thanks...

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andreyc
                      wrote on last edited by
                      #10

                      I'm glad I helped you.
                      If you think that the issue is resolved then add "[SOLVED]" to the title of your original message, please. So other people may find it useful.

                      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