For a web application, database connection should be made when SQL activity is required and close it when it is done. So I plan to make a function under WebSession:
LoadTable(Loc_Sql as string) as RecordSet
dim LocDB as new mySQLDatabase //MySQLCommunityServer
LocDB.Host = app.HostName
LocDB.Port = app.Port
LocDB.UserName = app.UserName
LocDB.Password = app.Password
if Not LocDB.Connect Then
MsgBox "Connect Error: " + LocDB.ErrorMessage
else
LocDB.SQLExecute("set names utf8 collate utf8_general_ci")
LocDB.SQLExecute("use " + app.UnicornDatabaseName)
dim rs as RecordSet
rs = LocDB.SQLSelect(Loc_Sql)
if rs = nil then
MsgBox "SQL Error: " + LocDB.ErrorMessage
end if
return rs
end if
End Select
Question: will LocDB close by itself after the rs is returned? I tried to add LocDB.close before “return rs”, the calling module receives an empty rs.
You are right, the trouble does not comes from the return rs line.
the code you share miss some line:
End Select
End something it does not have open. aka: you do not have any Select Case line shared.
LoadTable(Loc_Sql as string) as RecordSet
Probably miss Dim
Loc_Sql is not declared before used.
OK, I stop here: nearly evey line have an error when I try to compile it (Xojo 2017r1.1).
Please, share correct code so maybe someone can help.
Nota: to really help, some have to put the code somewhere (either create a brand new project and add plenty useless files that belong to this project to the boot disk or add code to a current project ).
DB.Close will destroy the recordset, at least with mysql. If you don’t close the database, then after the database object goes out of scope it will live until the recordset also goes out of scope, at which point both will be cleaned up.