Filterizing the database
David Lantsman -- davidlan@luckynet.co.il Monday, February 10, 1997 Environment: Visual C++ 4.0 Windows 95 Hello, I have my own class, derived from CDaoRecordset. When I open it I'd like to read only a small number of fields, those which are equal to something - CMyDaoRecordset *m_pSet = new CMyDaoRecordset(NULL); m_pSet->m_strFilter = "ColumnName = "+m_someStr; m_pSet->Open(); Now, when I read the rows of the record set, I see that nothing was filterized, and the database apperas as is. What can I do/ what did I do that was wrong to filterize the set? Thank you in advance, David Lantsman ================================== Lantiv, David Lantsman http://www.luckynet.co.il/~davidlan davidlan@luckynet.co.il ==================================
SCS.010@mch.scn.de Tuesday, February 11, 1997 [Mini-digest: 2 responses] Hi David, Try m_pSet->m_strFilter = "ColumnName = " + CString("'") + m_someStr + "'"; It should work. Tarun Mehta. -----From: "Dana M. Epp"When filtering for a Query... would you not want to place single quotes around a field which is a string literal.(I am guessing this as the field name is Column NAME). Try this.... CMyDaoRecordset *m_pSet = new CMyDaoRecordset(NULL); m_pSet->m_strFilter = "ColumnName = \'" m_pSet->m_strFilter += m_someStr; m_pSet->m_strFilter += "\'"; m_pSet->Open(); At 05:31 AM 2/10/97 +0200, you wrote: >Environment: Visual C++ 4.0 Windows 95 > >Hello, > >I have my own class, derived from CDaoRecordset. > >When I open it I'd like to read only a small number of >fields, those which are equal to something - > >CMyDaoRecordset *m_pSet = new CMyDaoRecordset(NULL); >m_pSet->m_strFilter = "ColumnName = "+m_someStr; >m_pSet->Open(); > >Now, when I read the rows of the record set, I see that nothing >was filterized, and the database apperas as is. > >What can I do/ what did I do that was wrong to filterize the >set? > >Thank you in advance, > David Lantsman > >================================== >Lantiv, David Lantsman > >http://www.luckynet.co.il/~davidlan >davidlan@luckynet.co.il >================================== > > PC'ing you, Dana M. Epp eppdm@uniserve.com http://bedrock.cyberhq.com/dana "How can one work with the technology of today, using yesterdays software and methods, and still be on the leading edge tomorrow? Why settle for less... I won't! "
Mike Blaszczak -- mikeblas@nwlink.com Wednesday, February 12, 1997 At 09:56 2/11/97 GMT, SCS.010@mch.scn.de wrote: >Hi David, > >Try >m_pSet->m_strFilter = "ColumnName = " + CString("'") + m_someStr + "'"; >It should work. > >Tarun Mehta. You might enjoy more spare time if you didn't abuse CString so. You might find that m_pSet->m_strFilter = "ColumnName = '" + m_someStr + "'"; to be somewhat more reasonable. If m_someStr ever contains a tick, though, you're still in trouble. .B ekiM http://www.nwlink.com/~mikeblas/ These words are my own. I do not speak on behalf of Microsoft. This performance was not lip-synched.
Erik Thomas -- ErikThomas@msn.com Thursday, February 13, 1997 [Mini-digest: 3 responses] >You might enjoy more spare time if you didn't abuse CString so. You >might find that > m_pSet->m_strFilter = "ColumnName = '" + m_someStr + "'"; >to be somewhat more reasonable. If m_someStr ever contains a tick, >though, you're still in trouble. If you make "ColumnName=" a string resource (without the quotes), you never have to worry about the contents of m_someStr. m_pSet->m_strFilter.Format(IDS_FILTERSTRING, m_someStr); Erik Thomas .B ekiM http://www.nwlink.com/~mikeblas/ These words are my own. I do not speak on behalf of Microsoft. This performance was not lip-synched. -----From: Roger Onslow/Newcastle/Computer Systems Australia/AU >>At 09:56 2/11/97 GMT, SCS.010@mch.scn.de wrote: >>>Hi David, >>>Try >>>m_pSet->m_strFilter = "ColumnName = " + CString("'") + m_someStr + "'"; >>>It should work. >>>Tarun Mehta. >>You might enjoy more spare time if you didn't abuse CString so. You >>might find that >> m_pSet->m_strFilter = "ColumnName = '" + m_someStr + "'"; >>to be somewhat more reasonable. If m_someStr ever contains a tick, >>though, you're still in trouble. >Further to above, declare yourself a global... >CString Quoted(const CString& value); // quote a string, doubling internal >quotes >and here's a simple routine to do that... so a safe version of above becomes > m_pSet->m_strFilter = "ColumnName = " + Quoted(m_someStr); Passing input args as LPCTSTR is usually better than using "const CString&" because it saves an (expensive) CString constructor (when a literal string is passed, or another LPCTSTR), but only involves an (inexpensive) cast to LPCTSTR when a CString is the acutal arg. If you REALLY need CString functionality for the arg within the fn (which often you won't) then you can always create a local CString variable instead. Of course, this counters the advantage of using LPCTSTR in the first place, BUT it does mean all your input string args are the same - and avoids some weird bugs I've seen with CString args calling incorrect constructors. BTW, notice that all input args in the CString methods themselves use LPCTSTR instead of "const CString&" for the same reasons. Of course, in your example, LPCTSTR would not help much because you use some CString functions on the input arg, Also, why not use the "Find" function to find the internal quote characters, rather than your own for loop and string indexing. Even better, why not use "SpanExcluding". Because MFC CStrings don't allow a starting position for find and span functions, you have to remove the leasing part of the string to search the rest of the string (using "Mid" function). Because the string to search/parse therefore needs to be modified, you need to take a copy of it, so a LPCTSTR arg is ok in this case. So you could end up with a function like this... (have'nt compiled it, just off the top of my head) CString Quoted(LPCTSTR szValue) { // A value in a db field needs to be quoted (eg 'MYVALUE') when used in a query // Also, if it happens to have a quote character in it already, then the quote // must be doubled up to give valid syntax (eg 'JB''S TIMBER DOORS) #define QUOTE "'" CString rest = szValue; int maxpossible = rest.GetLength()*2+2; CString quoted; quoted.GetBufferSetLength(maxpossible); quoted.ReleaseBuffer(); do { CString first = rest.SpanExcluding(QUOTE)); quoted += QUOTE + first; rest = rest.Mid(first.GetLength()); } while (! rest.IsEmpty()); quoted += QUOTE; quoted.FreeExtra(); return quoted; } NOTE: I've ued GetBufferSetLength, ReleaseBuffer and FreeExtra to pre-allocate a buffer big enough to hold the quoted string so there are no memory re-allocation overheads as the quoted string is build up. Of course, if you want REAL speed, build up the quoted string char by char with a for loop through the LPCTSTR arg (no calls to CString functions) Roger Onslow -----From: "Ronald D. Patton"Or if m_someStr contains MBCS or Unicode data you might consider: m_pSet->m_strFilter = _T("ColumnName = '") + m_someStr + _T("'"); Ron Patton ---------- > From: Mike Blaszczak > To: mfc-l@netcom.com > Cc: davidlan@luckynet.co.il > Subject: re: Filterizing the database > Date: Wednesday, February 12, 1997 10:32 AM > > At 09:56 2/11/97 GMT, SCS.010@mch.scn.de wrote: > >Hi David, > > > >Try > >m_pSet->m_strFilter = "ColumnName = " + CString("'") + m_someStr + "'"; > >It should work. > > > >Tarun Mehta. > > You might enjoy more spare time if you didn't abuse CString so. You > might find that > > m_pSet->m_strFilter = "ColumnName = '" + m_someStr + "'"; > > to be somewhat more reasonable. If m_someStr ever contains a tick, > though, you're still in trouble. > > > > > > > .B ekiM > http://www.nwlink.com/~mikeblas/ > These words are my own. I do not speak on behalf of Microsoft. > This performance was not lip-synched.
Become an MFC-L member | Вернуться в корень Архива |