Beginner Tutorial: Using SQLite with BlitzMax

BlitzMax Forums/BlitzMax Tutorials/Beginner Tutorial: Using SQLite with BlitzMax

I have been meaning to learn how to use the SQLite modules that have been written for BlitzMax. I finally had the excuse to do this as an application I'm writing required a database engine.

Special thanks to TeaMonkey and Rene Aye whose examples I studied to figure out how to use the module.

The Tutorials are in two parts: Tutorial 1 and Tutorial 2.

**Added Part 3

Have Fun.

wow ... thank you!!

as ever, a well laid out and clear tutorial, well done!

Great tutorial!!
Maybe you could add (in this thread or in the tutorial) how we can use SQLite for games written with BlitzMax. Some clear examples of application could clarify the use of this tool!

RepeatUntil,
I have no experience using this in a game but databases are not known for their speed. Quote from TeaMonkey's site
Be warned that a database query is pretty slow. Prepared statements are a big performance boost, as are memory tables, but they're still "slow" as far as a realtime game is concerned so try and keep your main loop queries to a minimum.

I remembered reading an article about SQLite and game programming but cannot locate it now :(

Thank you for your very informative SQLite tutorial
I have a question:
when in need to use a SQL function in a query (eg MAX(), COUNT(),...) how can I manage its result?
in the following example of SQL query the value returned by the query shold be the total number of records; I understand that a callback function works on all subsequent records in a query; how can I manage the result of COUNT() function, eg print or store it in a variable?
--- query example (general) ----
SELECT COUNT(field) FROM database
the query shold return the number of records in database "database" where the field "field" is not null (this is my guess)

in BlitzMax we should have something like this:
rc = sqlite3_exec(db, "SELECT COUNT(field) FROM database",callback, Null, Null)

If I don't use a callback function ( ... null,null,null) how can I manage the result of SELECT statement?
If I use a callback function (normally called on every record) what are the parameters to use (where is the returned value stored)?
bye
allox

"I have no experience using this in a game but databases are not known for their speed. Quote from TeaMonkey's site"

Please keep in mind when you say databases are not known for speed that is relative to what you are doing.

If you have 5 terebytes of data I bet a database will beat out your c routine. Unless your right the same logic that the database uses to scrub datafiles for data. Which in that case you would be writing a database engine. A database may not be good for real time data most of the time, it excels at mountians of data.

@oraclelover - did not mean to poke an ant's nest. The speed is relative as you say.

@allos, I've added a part 3 to answer your question. Take a look Part 3. Hope that answers your questions. AFAIK you need to use callbacks to get the result of the query.

You can get results without using a callback using something like this:

Local StatementHandle:int
Local Tail: Byte Ptr

' Executes the Command
Local ErrorCode:int = SQLite3_Prepare(db, Command, Command.Length, StatementHandle, Varptr(Tail))

' Make sure it executed correctly
If ErrorCode = 0 then

	' The Tail contains any part of the Command which wasn't executed
	' I've never had reason to use this myself
	Local UnusedSQL:string = string.FromCString(Tail)

	' Loop round all the rows in the resulting data set	
	Local Done = False
	Repeat
			
		' Move to the next row in the data set
		Local StepResult:Int = SQLite3_Step(StatementHandle)
			
		Select StepResult
		
			' Step has found the next row
			Case 100
				' If for example the first column is an integer
				Print SQLite3_Column_Int(StatementHandle, 0)
				' The next column is a string
				Print SQLite3_Column_Text(StatementHandle, 1)
				' The next column is a float
				Print SQLite3_Column_Double(StatementHandle, 2)
				
			' Step has reached the end of the data set
			Case 101
				Done = true
				
			' Step has resulted in an error
			Default
				RuntimeError(StepResult)
				
		End Select
	
	Until Done
	
	' Closes the data set
	SQLite3_Finalize(StatementHandle)
	
End If

Note that this code has been extracted from SQLite wrapper classes I was working on with the BMX v1.09 demo. The demo expired months ago and I haven't bought BMX yet so I haven't been able to finish the wrapper classes or test this code :-)

I've been planning to release my SQLite wrapper classes once I've bought BMX and finished them off :-)

Edit: As for using SQLite in games - I'm using it (in Blitz3D) for storing player profiles (options, high scores, stats etc) in Eridani. I guess I could use it for storing level data too, but then I'd have to write an editor for it and I find it much easier to just put that stuff into XML files.

thank you Assari, I knew my question would have found an answer
great! (see you at the next stop)

bye
Allos

@HappyCat, thanks for that info. I've yet to try out the SQLite3_prepare and its associated functionalities. I now understand a bit better how that works.

@allos, you're welcomed.

Just trying to run part 1 but get this when compiling:

Linking:untitled1.debug.exe
C:/Program Files/BlitzMaxDemo/tmp/.bmx/untitled1.bmx.gui.debug.win32.o(code+0x47): undefined reference to `__bb_sqlite_sqlite_'
Build Error: Failed to link C:/Program Files/BlitzMaxDemo/tmp/untitled1.debug.exe


I've put TeaMonkey's mod in the mod folder but I'm just using the BlitzMax demo, so i'm not sure if that's an issue. Any ideas?

No worries, it works fine in the new version.

I'm running BlitzMax demo version 1.12(downloaded it yesterday).
I downloaded both versions of SQLite for BlitzMax.
(tm.mod and user.mod)
When running the example program in tutorial 1 I get the following error when using tm.mod

Linking:untitled2.debug.exe
C:/Program Files/BlitzMaxDemo/tmp/.bmx/untitled2.bmx.gui.debug.win32.o(code+0x47): undefined reference to `__bb_sqlite_sqlite_'
Build Error: Failed to link C:/Program Files/BlitzMaxDemo/tmp/untitled2.debug.exe
Process complete


When using user.mod I get the following error...

Linking:sqlite_trial.debug.exe
C:/Program Files/BlitzMaxDemo/bin/ld.exe: cannot find C:/Program Files/BlitzMaxDemo/mod/user.mod/sqlite.mod/sqlite.debug.win32.a
Build Error: Failed to link C:/Program Files/BlitzMaxDemo/sqlite_trial.debug.exe
Process complete

Neither one works for me. Apparently I just don't have the files it is looking for? I'm mystified.

Thanks for reading this!