Game Development Community

dev|Pro Game Development Curriculum

SQLite integeration for Linux

by Dreamer · 03/03/2005 (11:36 am) · 7 comments

NOTE: What follows works with a clean build of TGE 1.3, under Gentoo it should work as-is for other distros as well.

Ok to start with you will need to get SQLite installed on your system, the easiest way to do this is to check with your distro for an RPM if you are running an RPM based distro.
If you are unsure or unable to find an RPM for your distro, then go to http://www.rpmfind.net/ and do a search there for SQLite.

Gentoo users only need to emerge sqlite on the command line
Debian would be apt-get sqlite (If I'm not mistaken)

We will use 2.x version SQLite, since that is what I currently have running. If for some reason you REALLY want a 3.x then you will need to do alot more editing.

Once you have it installed on your system open a console window
mkdir ~/Torque/lib/sqlite
cp /usr/include/sqlite.h ~/Torque/engine/console
cp /usr/lib/sqlite.a ~/Torque/lib/sqlite/sqlite_DEBUG.a
open your favorite text editor edit ~/Torque/engine/targets.torque.mk

Find
SOURCE.CONSOLE=\
	console/compiledEval.cc \
	console/compiler.cc \
	console/console.cc \
	console/consoleDoc.cc \
	console/consoleFunctions.cc \
	console/consoleInternal.cc \
	console/consoleLogger.cc \
	console/consoleObject.cc \
	console/consoleTypes.cc \
	console/gram.cc \
	console/scan.cc \
	console/scriptObject.cc \
After console/scriptObject \
add
console/SQLiteObject.cc \

In the same file look for
INCLUDES_BASE = -I../lib/zlib -I../lib/lungif -I../lib/lpng -I../lib/ljpeg -I../lib/directx8 -I../lib/vorbis/include

replace with
INCLUDES_BASE = -I../lib/zlib -I../lib/lungif -I../lib/lpng -I../lib/ljpeg -I../lib/directx8 -I../lib/vorbis/include -I../lib/sqlite

Continuing
Look for
$(PRE.LIBRARY.LIB)ljpeg$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)lpng$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)lungif$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)zlib$(EXT.LIB)
Replace with
$(PRE.LIBRARY.LIB)ljpeg$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)lpng$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)lungif$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)sqlite$(EXT.LIB) \
	$(PRE.LIBRARY.LIB)zlib$(EXT.LIB)

The above is in 2 seperate places in the script, you need to replace it in both places.
Save your file and exit.

Now as you noticed there was a file called SQLiteObject.cc, we will also be using SQLiteObject.h both of which are in the download from the other SQLite tutorial, which is meant for windows users.
The download is at
http://www.garagegames.com/uploaded/code/5531.sqliteobject.zip

The tutorial is at
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5531

A word of caution on the download, you will ONLY be able to use the SQLiteObject.cc, SQLiteObject.h & sqlite.cs files.

DO NOT UNDER ANY CIRCUMSTANCES USE THE OTHER FILES. They won't work under linux.

Ok that said open up SQLiteObject.cc
Look for
#include <STDLIB.H>
replace with
#include <STDLIB.H>

Save and close
From a console
make clean
make -f mk/configure.mk OS=LINUX COMPILER=GCC3 BUILD=DEBUG
make
You WILL get an error thats ok
Next you will
cp ~/Torque/lib/sqlite/sqlite_DEBUG.a ~/Torque/lib/out.GCC3.DEBUG
make

And with that it should compile cleanly
Move your sqlite.cs file to examples and add
exec("sqlite.cs");

To init.cs

There ya go, SQLite integration in torque under linux.
Special thanx to John Vanderbek and Entr0py for the ideas and code.

Hope this helps!

#1
03/05/2005 (7:11 am)
I use sqlite in my cross platform app... not a game... As it works under OSX, Linux, and PC, have you tried to get it to work in a cross-platform Torque game?
#2
03/05/2005 (10:58 pm)
Well yes there is no reason for this not to work in a cross platform environment, as long as you are using cross platform tools.
I recommend using TBE for Windows, and Eclipse on Linux and Mac OSX.

The easiest way to do this is to create a new eclipse project, select "Managed Make files" then select the Torque SDK as the project directory.

This way any changes you make on one platform will carry over to all of the others.

The way I have described above should work just fine under TBE in windows, so at least we have Linux and Windows support covered, as for OSX I don't think there is any OS specific code that would have to be modified for this to work.
#3
01/26/2006 (4:06 am)
with fedora-4 i installed sqlite2 ( version 2.8.16 ) and sqlite2-devl useing yum ... their is no sqlite.a file on my system ( the h is their ) and i did a find / -name sqlite.* only to get
/var/cache/man/cat1/sqlite.1.bz2
/usr/lib/pkgconfig/sqlite.pc
/usr/src/torque/cvs/torque/engine/console/sqlite.h
/usr/share/man/man1/sqlite.1.gz
/usr/share/pear/DB/sqlite.php
/usr/share/doc/sqlite-devel-3.1.2/doc/sqlite.gif
/usr/share/doc/sqlite-devel-3.1.2/doc/sqlite.html
/usr/share/doc/sqlite2-devel-2.8.16/sqlite.html
/usr/include/sqlite.h
#4
12/23/2007 (2:47 pm)
Problem with consolemethod "query" function:
// iLen should now be the length of our new string
         szNew = new char[iLen];
should be:
// iLen should now be the length of our new string
         szNew = new char[iLen+1];
         szNew[iLen] = 0; // null terminate!

This did not crop up most queries until I tried this query:
SELECT sql FROM sqlite_master WHERE type='table' AND name='?'

Please test this change before and after to see if this is a typical issue.

Nick,
I would suggest just compiling in all the source files with the engine source files and not making a library. The licensing of sqlite is not lgpl, but is released to public domain. So this is not an issue when it comes to distribution.
#5
12/23/2007 (3:00 pm)
Here is a better version of my change:
// iLen should now be the length of our new string
         szNew = Con::getReturnBuffer(iLen+1);
         szNew[iLen] = 0; // null terminate!

This should prevent a memory leak.
#6
12/23/2007 (3:20 pm)
Here is a problem with the getcolumn consolemethod:
pResultSet = object->GetResultSet(dAtoi(argv[2]));
   if (pResultSet)
   {
Should be:
pResultSet = object->GetResultSet(dAtoi(argv[2]));
   if (pResultSet && pResultSet->iNumCols)
   {
This way if you request a column by name that does not exist the engine does not crash. There may be other issues like this with other consolefunctions so be on the lookout.
#7
12/29/2007 (12:43 pm)
Found another bug.

This code in function "getColumnName":
iColumn = dAtoi(argv[3]);
      if (iColumn == 0)
         return "";  // column indices start at 1, not 0

      // now we should have an index for our column name
      if (pRow->vColumnNames[iColumn])
         return pRow->vColumnNames[iColumn];
      else
         return "";
Should be:
iColumn = dAtoi(argv[3]);
      if (iColumn == 0)
         return "";  // column indices start at 1, not 0

      // We temporarily padded the index in GetColumnIndex() so we could return a 
      // 0 for error.  So now we need to drop it back down.
      iColumn--;

      // now we should have an index for our column name
      if (pRow->vColumnNames[iColumn])
         return pRow->vColumnNames[iColumn];
      else
         return "";

Otherwise you get a seg fault. :)