Database.SQLSelect

From Xojo Documentation

Method

Database.SQLSelect(SelectString as String) As RecordSet

Supported for all project types and targets.

This is an SQL statement that returns a RecordSet. SelectString contains the SQL statement.

Notes

Typically only SQL SELECT statements return a RecordSet, but some databases return a RecordSet for SQL commands such as INSERT, UPDATE or stored procedures.

If the SQL does not return data then Nil is returned. Nil is also usually returned if there is an error in the SQL statement, but you should instead check Database.Error to check if an error occurred.

To avoid SQL Injection, be sure to use Prepared SQL Statements.

Sample Code

This code gets the names of the tables in a SQLite database using its built-in 'sqlite_master' table:

Dim dbFile As FolderItem
dbFile = GetOpenFolderItem("") // Choose a SQLite DB

Dim db As New SQLiteDatabase
db.DatabaseFile = dbFile

If db.Connect Then
Dim rs As RecordSet
rs = db.SQLSelect("SELECT * FROM sqlite_master;")

If db.Error Then
MsgBox("Error: " + db.ErrorMessage)
Return
End If

If rs <> Nil Then
While Not rs.EOF
If rs.Field("type").StringValue = "table" Then
MsgBox("Table: " + rs.Field("name").StringValue)
End If
rs.MoveNext
Wend
rs.Close
End If
db.Close
Else
If db.Error Then
MsgBox("Error: " + db.ErrorMessage)
Return
End If
End If