[SOLVED] function with a defaul QVector parameter value
-
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 -
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.
-
-
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); -
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;
}
@ - ATP_db.h
-
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. -
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
-
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;
}
@ -
Thanks to andreyc we just have an updated source code...
Many thanks...