cycotron Posted July 28, 2010 Share Posted July 28, 2010 In the past, I used the function like below, similar to the documentation example: _SQLite_Query (-1, "SELECT * FROM table1 WHERE UserName LIKE '" & $Username & "%';", $hQuery) $hrow=0 While _SQLite_FetchData ($hQuery, $SQLFetchUsernameArray) = $SQLITE_OK WEnd _SQLite_QueryFinalize ( $hQuery ) $var = $SQLFetchUsernameArray[1] After upgrading to AutoIt 3.3.6.1, this doesn't work. $SQLFetchUsernameArray is wiped out after the while loop ends, so all of my code would have to be rewritten to include the operations in the SQL while loop. I just discovered that, I can do it without the While loop, like shown here (which would make for less code and an easier update to the new AutoIt version): _SQLite_Query (-1, "SELECT * FROM table1 WHERE UserName LIKE '" & $Username & "%';", $hQuery) _SQLite_QueryFinalize ( $hQuery ) $var = $SQLFetchUsernameArray[1] Is there anything wrong with not using a While loop to fetch the SQL data? Am I missing something here? Link to comment Share on other sites More sharing options...
PsaltyDS Posted July 28, 2010 Share Posted July 28, 2010 It looks like the loop is only so you can select the last matching record. The loop shouldn't be required. If you use ORDER BY to sort the results as required, the one you want should be the first record found. Or refine the query to only get one result row to begin with. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
cycotron Posted July 29, 2010 Author Share Posted July 29, 2010 Perfect. That was only a code snippet, the $username variable is passed to the function and there should only be one row as a result. I checked the Array and it does contain all fields of info for the row, so I should be golden. Thanks for the confirmation. I was banging my head against this one for awhile until I figured out the While loop was the culprit! Link to comment Share on other sites More sharing options...
jchd Posted July 30, 2010 Share Posted July 30, 2010 You're making your life harder than it should be. Use the higher level functions when applicable: in your case (you assert the resultset will have a single row) use _SQLite_QuerySingleRow. All will be done in one call without extra fuss or questions. I don't know why many people insist on decomposing these high-level calls and invoke themselves lower level functions. Most of the times there are subtle error conditions they ignore, which are handled efficiently and rigorously (I cross fingers about that) in the UDF code. Moretheless the UDF code is often faster than most homebrew loops I happen to see. Now to answer the implicit question about why the change was made: it appeared more consistent to void the result variable when the end of resultset was hit, rather than leaving the last row "live" while it shouldn't be. In short, _past_ the last page of a book, I don't read the ... last page anymore: I can read nothing. There is another change in _SQLite_FetchData: it will finalize the query (as before) by default but you now have a parameter to make it _not_ finalize the query, so that people may call _SQLite_QueryReset and rescan the resultset in the same transaction or, more exactly, as part of the same SQL operation. That was not possible before. Don't forget to finalize explicitely _all_ open query when you don't need them anymore. Not doing so will result in an SQLite error. Don't forget that while a query is open (not finalized) the database is write-locked so this starves any concurent writer. BTW you definitely should escape any (text) string litteral passed as part of an SQL starement. In your example, if you happen to have $username = "Mike O'Connor" then that will cause an error in SQLite as the internal single quote should have been doubled. Here are two new functions _SQLite_FastEscape and _SQLite_FastEncode which do the job for text/binary respectively with significant time improvement over the older functions. They're ready to appear in the next beta/release. ; #FUNCTION# ==================================================================================================================== ; Name...........: _SQLite_FastEncode ; Description ...: Encodes Binary data (exclusively) for use as BLOB in SQLite statements ; Syntax.........: _SQLite_FastEncode($vData) ; Parameters ....: $vData - Data to be encoded (Binary only) ; Return values .: On Success - Returns Encoded data to be stored as BLOB in SQLite ; On Failure - Returns Empty String ; @error Value(s): 1 - Data is not a binary type ; Author ........: jchd ; =============================================================================================================================== Func _SQLite_FastEncode($vData) If Not IsBinary($vData) Then Return SetError(1, 0, "") Return "X'" & Hex($vData) & "'" EndFunc ;==>_SQLite_FastEncode ; #FUNCTION# ==================================================================================================================== ; Name...........: _SQLite_FastEscape ; Description ...: Encodes a string or number for use as TEXT in SQLite statements ; Syntax.........: _SQLite_Escape($sString) ; Parameters ....: $sString - String to escape. ; Return values .: On Success - Returns Escaped String ; On Failure - Returns Empty String ; @error Value(s): 1 - Data is not a string (or a numeric) ; Author ........: jchd ; =============================================================================================================================== Func _SQLite_FastEscape($sString) If IsNumber($sString) Then $sString = String($sString) ; don't raise error if passing a numeric parameter If Not IsString($sString) Then Return SetError(1, 0, "") Return ("'" & StringReplace($sString, "'", "''", 0, 1) & "'") EndFunc ;==>_SQLite_FastEscape This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
dmob Posted July 31, 2010 Share Posted July 31, 2010 Very informative. I also wasn't sure about the usage of the function, and I use it a lot. Thanks for the explanation. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now