1

I have a call to QSqlQuery which returns no rows when I use QSqlQuery::bindValue, but does return rows when I add the values in with QString::arg.

Specifically, this returns nothing:

QSqlQuery q(QSqlDatabase::database( mDbAdapter->dbFilename() ));
q.prepare("select Focus.TextFormId as ID, Focus, TextForm, Gloss from (select TextFormId,group_concat( Transcription , ' ' ) as TextForm, group_concat( Gloss , '-' ) as Gloss from (select TextFormId,AllomorphId,Allomorph.Form as Transcription,LexicalEntryGloss.Form as Gloss from MorphologicalAnalysisMembers,Allomorph,LexicalEntryGloss where TextFormId in ( select TextFormId from MorphologicalAnalysisMembers where AllomorphId in (select _id from Allomorph where  LexicalEntryId=:LexicalEntryId and WritingSystem=:TextFormWS) ) and AllomorphId = Allomorph._id and Allomorph.LexicalEntryId = LexicalEntryGloss.LexicalEntryId and LexicalEntryGloss.WritingSystem=:GlossWS order by TextFormId, AllomorphOrder) group by TextFormId ) as Concatenation left join  ( select TextFormId, Form as Focus from Allomorph,MorphologicalAnalysisMembers on Allomorph._id=MorphologicalAnalysisMembers.AllomorphId and LexicalEntryId=:LexicalEntryId ) as Focus on Focus.TextFormId = Concatenation.TextFormId;");
q.bindValue(":LexicalEntryId", mLexicalEntryId);
q.bindValue(":GlossWS", mGlossWs.id());
q.bindValue(":TextFormWS", mTextFormWs.id());
if( !q.exec() )
    qWarning() << q.lastError().text() << q.executedQuery();

Whereas this returns the proper result

QSqlQuery q(QSqlDatabase::database( mDbAdapter->dbFilename() ));
q.prepare(
        QString("select Focus.TextFormId as ID, Focus, TextForm, Gloss from (select TextFormId,group_concat( Transcription , ' ' ) as TextForm, group_concat( Gloss , '-' ) as Gloss from (select TextFormId,AllomorphId,Allomorph.Form as Transcription,LexicalEntryGloss.Form as Gloss from MorphologicalAnalysisMembers,Allomorph,LexicalEntryGloss where TextFormId in ( select TextFormId from MorphologicalAnalysisMembers where AllomorphId in (select _id from Allomorph where  LexicalEntryId=%1 and WritingSystem=%3) ) and AllomorphId = Allomorph._id and Allomorph.LexicalEntryId = LexicalEntryGloss.LexicalEntryId and LexicalEntryGloss.WritingSystem=%2 order by TextFormId, AllomorphOrder) group by TextFormId ) as Concatenation left join  ( select TextFormId, Form as Focus from Allomorph,MorphologicalAnalysisMembers on Allomorph._id=MorphologicalAnalysisMembers.AllomorphId and LexicalEntryId=%1 ) as Focus on Focus.TextFormId = Concatenation.TextFormId;")
        .arg(mLexicalEntryId).arg(mGlossWs.id()).arg(mTextFormWs.id())
        );
if( !q.exec() )
    qWarning() << q.lastError().text() << q.executedQuery();

It would be quite a bit to reproduce the entire database, but here is the query in a more intelligible form:

select Focus.TextFormId as ID, Focus, TextForm, Gloss from (select TextFormId,group_concat( Transcription , ' ' ) as TextForm, group_concat( Gloss , '-' ) as Gloss
from 
(select TextFormId,AllomorphId,Allomorph.Form as Transcription,LexicalEntryGloss.Form as Gloss 
    from MorphologicalAnalysisMembers,Allomorph,LexicalEntryGloss 
    where TextFormId in 
        ( select TextFormId from MorphologicalAnalysisMembers where AllomorphId in 
            (select _id from Allomorph where  LexicalEntryId=%1 and WritingSystem=%3) ) 
        and AllomorphId = Allomorph._id 
        and Allomorph.LexicalEntryId = LexicalEntryGloss.LexicalEntryId 
        and LexicalEntryGloss.WritingSystem=%2 
        order by TextFormId, AllomorphOrder) 
    group by TextFormId ) as Concatenation 
    left join  
    ( select TextFormId, Form as Focus from Allomorph,MorphologicalAnalysisMembers 
        on Allomorph._id=MorphologicalAnalysisMembers.AllomorphId and LexicalEntryId=%1 ) 
    as Focus on Focus.TextFormId = Concatenation.TextFormId;

Granted that there are probably ways to improve the query, I searched the documents and couldn't find any indication that QSqlQuery::bindValue has restrictions.

3
  • Did you try using binding values with different name? Commented Feb 4, 2013 at 18:08
  • Thank you. When I tried changing things around, that lead me to the solution. Commented Feb 6, 2013 at 9:07
  • Glad to help! You should post your answer and select it as accepted, to help the others! Commented Feb 6, 2013 at 10:38

1 Answer 1

1

I was eventually able to get this to work with binding by changing the the way I bound values. It works if I insert mLexicalEntryId twice using different placeholders (first example below), or if I use the ? notation for binding values (second example below). Though I otherwise prefer to use the placeholders, it feels silly to have two different placeholders for the same variable, so I will stick with ? notation.

q.prepare("select Focus.TextFormId as ID, Focus, TextForm, Gloss from (select TextFormId,group_concat( Transcription , ' ' ) as TextForm, group_concat( Gloss , '-' ) as Gloss from (select TextFormId,AllomorphId,Allomorph.Form as Transcription,LexicalEntryGloss.Form as Gloss from MorphologicalAnalysisMembers,Allomorph,LexicalEntryGloss where TextFormId in ( select TextFormId from MorphologicalAnalysisMembers where AllomorphId in (select _id from Allomorph where  LexicalEntryId=:One and WritingSystem=:Three) ) and AllomorphId = Allomorph._id and Allomorph.LexicalEntryId = LexicalEntryGloss.LexicalEntryId and LexicalEntryGloss.WritingSystem=:Two order by TextFormId, AllomorphOrder) group by TextFormId ) as Concatenation left join  ( select TextFormId, Form as Focus from Allomorph,MorphologicalAnalysisMembers on Allomorph._id=MorphologicalAnalysisMembers.AllomorphId and LexicalEntryId=:Four ) as Focus on Focus.TextFormId = Concatenation.TextFormId;");
q.bindValue(":One", mLexicalEntryId);
q.bindValue(":Two", mGlossWs.id());
q.bindValue(":Three", mTextFormWs.id());
q.bindValue(":Four", mLexicalEntryId);

q.prepare("select Focus.TextFormId as ID, Focus, TextForm, Gloss from (select TextFormId,group_concat( Transcription , ' ' ) as TextForm, group_concat( Gloss , '-' ) as Gloss from (select TextFormId,AllomorphId,Allomorph.Form as Transcription,LexicalEntryGloss.Form as Gloss from MorphologicalAnalysisMembers,Allomorph,LexicalEntryGloss where TextFormId in ( select TextFormId from MorphologicalAnalysisMembers where AllomorphId in (select _id from Allomorph where  LexicalEntryId=? and WritingSystem=?) ) and AllomorphId = Allomorph._id and Allomorph.LexicalEntryId = LexicalEntryGloss.LexicalEntryId and LexicalEntryGloss.WritingSystem=? order by TextFormId, AllomorphOrder) group by TextFormId ) as Concatenation left join  ( select TextFormId, Form as Focus from Allomorph,MorphologicalAnalysisMembers on Allomorph._id=MorphologicalAnalysisMembers.AllomorphId and LexicalEntryId=? ) as Focus on Focus.TextFormId = Concatenation.TextFormId;");
q.addBindValue(mLexicalEntryId);
q.addBindValue(mTextFormWs.id());
q.addBindValue(mGlossWs.id());
q.addBindValue(mLexicalEntryId);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.