void QxSqlQuery::resolveOutput(QSqlQuery & query, bool bFetchSqlResult) { bool bKey = (qx::QxSqlDatabase::getSingleton()->getSqlPlaceHolderStyle() != qx::QxSqlDatabase::ph_style_question_mark); //qDebug() << "start resolveOutput"; for (long l = 0; l < m_lstValue.count(); l++) { type_bind_value val = m_lstValue.getByIndex(l); if (val.get<1>() == QSql::In) { continue; } if (bKey) { val.get<0>() = query.boundValue(m_lstValue.getKeyByIndex(l)); } else { val.get<0>() = query.boundValue(l); } } //qDebug() << "finish resolveOutput"; if (bFetchSqlResult) { fetchSqlResult(query); } }
void QSqlQuery_snippets() { { // typical loop //! [7] QSqlQuery query("SELECT country FROM artist"); while (query.next()) { QString country = query.value(0).toString(); doSomething(country); } //! [7] } { // field index lookup //! [8] QSqlQuery query("SELECT * FROM artist"); int fieldNo = query.record().indexOf("country"); while (query.next()) { QString country = query.value(fieldNo).toString(); doSomething(country); } //! [8] } { // named with named //! [9] QSqlQuery query; query.prepare("INSERT INTO person (id, forename, surname) " "VALUES (:id, :forename, :surname)"); query.bindValue(":id", 1001); query.bindValue(":forename", "Bart"); query.bindValue(":surname", "Simpson"); query.exec(); //! [9] } { // positional with named //! [10] QSqlQuery query; query.prepare("INSERT INTO person (id, forename, surname) " "VALUES (:id, :forename, :surname)"); query.bindValue(0, 1001); query.bindValue(1, "Bart"); query.bindValue(2, "Simpson"); query.exec(); //! [10] } { // positional 1 //! [11] QSqlQuery query; query.prepare("INSERT INTO person (id, forename, surname) " "VALUES (?, ?, ?)"); query.bindValue(0, 1001); query.bindValue(1, "Bart"); query.bindValue(2, "Simpson"); query.exec(); //! [11] } { // positional 2 //! [12] QSqlQuery query; query.prepare("INSERT INTO person (id, forename, surname) " "VALUES (?, ?, ?)"); query.addBindValue(1001); query.addBindValue("Bart"); query.addBindValue("Simpson"); query.exec(); //! [12] } { // stored //! [13] QSqlQuery query; query.prepare("CALL AsciiToInt(?, ?)"); query.bindValue(0, "A"); query.bindValue(1, 0, QSql::Out); query.exec(); int i = query.boundValue(1).toInt(); // i is 65 //! [13] Q_UNUSED(i); } QSqlQuery query; { // examine with named binding //! [14] QMapIterator<QString, QVariant> i(query.boundValues()); while (i.hasNext()) { i.next(); cout << i.key().toUtf8().data() << ": " << i.value().toString().toUtf8().data() << endl; } //! [14] } { // examine with positional binding //! [15] QList<QVariant> list = query.boundValues().values(); for (int i = 0; i < list.size(); ++i) cout << i << ": " << list.at(i).toString().toUtf8().data() << endl; //! [15] } }
void FrmFrame::apply() { bool bError=false; //We call a stored procedure to see if there are GLS available outside the bin QSqlQuery query; query.setForwardOnly(true); int id= cmbPrexistent->model()->index(cmbPrexistent->currentIndex(),0).data().toInt(); int n=0; query.prepare("{CALL spCountGLS4Frame(?,?)}"); query.bindValue(0,id); query.bindValue("Number",n,QSql::Out); if (!query.exec()){ emit showError(query.lastError().text()); bError=true;; } n = query.boundValue("Number").toInt(); if (n<1){ emit showError(tr("There are no Group of Landing Sites for this frame!")); bError=true; }else{ //First insert the dates... if (!mapperStartDt->submit() || !mapperEndDt->submit()){ if (m_tDateTime->lastError().type()!=QSqlError::NoError) emit showError(m_tDateTime->lastError().text()); else emit showError(tr("Could not submit mapper!")); bError=true; } else{ if (!m_tDateTime->submitAll()){ if (m_tDateTime->lastError().type()!=QSqlError::NoError) emit showError(m_tDateTime->lastError().text()); else emit showError(tr("Could not write DateTime in the database!")); bError=true; } } while(m_tDateTime->canFetchMore()) m_tDateTime->fetchMore(); mapperStartDt->setCurrentIndex(m_tDateTime->rowCount()-2); mapperEndDt->setCurrentIndex(m_tDateTime->rowCount()-1); int startIdx=mapperStartDt->currentIndex(); int endIdx=mapperEndDt->currentIndex(); if (bError) { emit showError(tr("Could not create dates in the database!")); }else{ //Now insert the record while(tFrameTime->canFetchMore()) tFrameTime->fetchMore(); tFrameTime->insertRow(tFrameTime->rowCount()); QModelIndex idx=tFrameTime->index(tFrameTime->rowCount()-1,1);//id frame if (idx.isValid()){ int idFrame; if (getCurrentFrame(idFrame)){ tFrameTime->setData(idx,idFrame); QModelIndex idx=tFrameTime->index(tFrameTime->rowCount()-1,2);//start dt if (idx.isValid()){ int idStart; if (getDtId(startIdx,idStart)){ tFrameTime->setData(idx,idStart); idx=tFrameTime->index(tFrameTime->rowCount()-1,3);//end dt if (idx.isValid()){ int idEnd; if (getDtId(endIdx,idEnd)){ tFrameTime->setData(idx,idEnd); }else bError=true; } }else bError=true; }else bError=true; }else bError=true; }else bError=true; } bError=!tFrameTime->submitAll(); } if (!bError){ QList<QWidget*> lWidgets; lWidgets << this->groupPhysical; lWidgets << this->groupTime; emit lockControls(true,lWidgets); }else{ if (tFrameTime->lastError().type()!=QSqlError::NoError) emit showError(tFrameTime->lastError().text()); } pushNext->setEnabled(!bError); pushApply->setEnabled(bError); if (!bError) { emit showStatus(tr("Record successfully inserted in the database!")); m_submitted=true; while(tFrameTime->canFetchMore()) tFrameTime->fetchMore(); m_curFrameTime=tFrameTime->rowCount()-1; updateSample();//update sample here, because of the save } }