QSqlQuery不返回带有QSqlQuery :: bindValue的行,但是如果我使用QString :: arg插入值

时间:2013-02-04 14:42:41

标签: sql qt

我调用了QSqlQuery,在使用QSqlQuery :: bindValue时没有返回任何行,但在使用QString :: arg添加值时返回行。

具体来说,这不会返回任何内容:

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();

然而这会返回正确的结果

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();

重现整个数据库会有很多,但这里的查询更易于理解:

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;

虽然有可能改进查询的方法,但是我搜索了文档并找不到QSqlQuery :: bindValue有限制的任何迹象。

1 个答案:

答案 0 :(得分:1)

我最终能够通过改变绑定值的方式来实现绑定。如果我使用不同的占位符(下面的第一个示例)插入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=: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);