Wednesday, June 27, 2007

Adding SQLite to Run BASIC

Here is some example code that runs under Run BASIC using SQLite v3.4. I haven't pushed this stuff onto the publicly hosted site. SQLite will come with the Run BASIC personal server and should be suitable for many projects. Support for other databases like Oracle, PostgreSQL, SQL Server, etc. will be added with the professional server license.

sqliteconnect #users, "test.db"
call execAndPrint "select * from users"
call execAndPrint "select count(*) from users"
#users disconnect()
end

sub execAndPrint query$
#users execute(query$)

while #users hasanswer()
result$ = #users nextrow$(",")
print result$
wend
end sub

SQLite is a popular database engine provided as a library and is supported for Windows, Mac and Linux. The source code is available and is in the public domain so it is completely free. We will support the parts of Run BASIC that integrate with SQLite, but as for the specific level of conformance to the SQL standard and other technical details, see http://www.sqlite.org/

The SQLITECONNECT #obj, "databasefile.db" statement connects to a database and sets the #obj variable to be a connection object. The connection object understands EXECUTE(), HASANSWER(), NEXTROW$() and DISCONNECT(). I'm sure there will be more commands.

Notice the NEXTROW$() method. It returns each query result row as a string, with each item delimited by the specified string ("," in the example but it could be CHR$(9) or something else). It is trivial then to use the WORD$() function to extract the information. We will certainly also add the ability to get items by name.