In the following code: queryString is an array of valid SQLite select queries rsQuery is an array of recordsets
The principle is loop through the queries (queryString(n)) and generate a recordset (rsQuery(n)) for each one.
Database connection etc is active. All the basics are done. Problem is I am testing with just one query “Select * from Products” and it is raising an error without fail every time regardless of the query. near “S”: syntax error . Bizarre can anyone help I am pulling my hair out
[code] for N as integer = 0 to UBound(queryString)
if trim(queryString(n)) <> "" then
ReDim rsQuery(n)
rsQuery(N) = mDatabase.ActiveDatabase.SQLSelect(trim(queryString(N)))
if mDatabase.ActiveDatabase.Error then
MsgBox mDatabase.ActiveDatabase.ErrorMessage
Exit Sub
end if
ListRecordset(rsQuery(0),lvwQuery)
end if
next N[/code]
[quote=80947:@Peter Fargo]Doesn’t get much simpler than that. Have you tried it in a SQL query tool outside of Xojo?
Copy and paste the query from the debugger.[/quote]
Yes absolutely no problem getting rs back
Dim selectString As String = trim(queryString(N))
Break // check that selectString is correct by copying it from the debugger's variable text field and pasting
// into Navicat, SequelPro, or whatever tool you use to manage your databases.
And don’t forget to check the returned RecordSet for being Nil (directly after SQLSelect line).
Best guess: The query isn’t what you think it is. Add this to the error handler code:
if mDatabase.ActiveDatabase.Error then
MsgBox mDatabase.ActiveDatabase.ErrorMessage
System.DebugLog queryString( N )
System.DebugLog EncodeHex( queryString( N ), true )
Exit Sub
end if
Then check the log, especially the hex version. I suspect you have a hidden character in there.
Or use Eli’s approach, as he beat me to it.
BTW, are you sure you’ve properly connected to your database?
[quote=80960:@Kem Tekinay] if mDatabase.ActiveDatabase.Error then
MsgBox mDatabase.ActiveDatabase.ErrorMessage
System.DebugLog queryString( N )
System.DebugLog EncodeHex( queryString( N ), true )
Exit Sub
end if[/quote]
Mmm - debug log is showing query string to be “s” only. Bizarre because I have MsgBox just before that line and its showing the full query.
I could tell because UTF-16 uses a minimum of 2 bytes per character, hence the NULLs between the bytes. You can tell by checking the encoding in the debugger. Eli’s recommendation of pulling the string out of the array first will help with that.
BTW, rediminsioning the array as you go is slow. Redim before the loop instead.
redim rsQuery( queryString.Ubound )
for N as integer = 0 to UBound(queryString)
dim thisQuery as string = queryString( N ).ConvertEncoding( Encodings.UTF8 ).Trim
if thisQuery <> "" then
rsQuery(N) = mDatabase.ActiveDatabase.SQLSelect( thisQuery )