SQLite3 Sample

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
bayo
Posts: 4
Joined: Mon Feb 11, 2008 5:04 am
Location: UK

SQLite3 Sample

Post by bayo »

Hello all guys,

I'm developing a mmorpg game for PSP and I want to save the character data in a DB.

I saw SQLite3 for PSP and I try to use it, but I can't insert values into a table. I know how to create the db and create a table, but I can't put this query in the DB: INSERT INTO pj (id, lvl, hp, mp) VALUES (1, 1, 100, 60)

For this, I need a example with creating table, insert values into table and selecting data.

Thanks in advance ;)
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)

Code: Select all

#include <pspkernel.h>
#include <pspctrl.h>
#include <psphprm.h> 
#include <pspdebug.h>
#include <psppower.h>
#include <stdio.h>
#include <string.h>
#include <pspsdk.h>
#include <unistd.h>
#include <sqlite3.h>

#define RGB&#40;r, g, b&#41; &#40;&#40;b << 16&#41; | &#40;g << 8&#41; | r&#41;
#define BLACK RGB&#40;0, 0, 0&#41;
#define WHITE RGB&#40;255, 255, 255&#41;

PSP_MODULE_INFO&#40;"SQLite Test", 0, 1, 0&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;
//PSP_HEAP_SIZE_KB&#40;22000&#41;;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Callbacks&#58;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
    sceKernelExitGame&#40;&#41;;
    return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
    int cbid;

    cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
    sceKernelRegisterExitCallback&#40;cbid&#41;;

    sceKernelSleepThreadCB&#40;&#41;;

    return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
    int thid = 0;

    thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
    if&#40;thid >= 0&#41; &#123;
            sceKernelStartThread&#40;thid, 0, 0&#41;;
    &#125;

    return thid;
&#125;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SQLite callback&#58;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int SQLiteCallback&#40;void *NotUsed, int argc, char **argv, char **azColName&#41;&#123;
    int i;
    for&#40;i=0; i<argc;i++&#41;&#123;
        pspDebugScreenPrintf&#40;"%s = %s\n", azColName&#91;i&#93;, argv&#91;i&#93; ? argv&#91;i&#93; &#58; "NULL"&#41;;
    &#125;
    pspDebugScreenPrintf&#40;"\n"&#41;;
	return&#40;0&#41;;
&#125;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main&#58;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main&#40;&#41; &#123;
	char dbFile&#91;257&#93; = "";
    char sql&#91;1000&#93; = "";
	int retValue;
	char *zErr = 0;

	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_ANALOG&#41;;
	SetupCallbacks&#40;&#41;;

    sprintf&#40;dbFile, "./test.db"&#41;; 

	pspDebugScreenInit&#40;&#41;;
	pspDebugScreenSetTextColor&#40;WHITE&#41;;
	pspDebugScreenSetBackColor&#40;BLACK&#41;;

	//Create an empty db&#58;
	pspDebugScreenSetXY&#40;0, 0&#41;;
	pspDebugScreenPrintf&#40;"Creating empty database %s...", dbFile&#41;;
	sqlite3 *db;
	retValue = sqlite3_open&#40;dbFile, &db&#41;;
    if &#40;retValue&#41;&#123;
		pspDebugScreenPrintf&#40;"\n"&#41;;
		pspDebugScreenPrintf&#40;"Error creating database&#58; %s", sqlite3_errmsg&#40;db&#41;&#41;;
	&#125;else&#123;
        pspDebugScreenPrintf&#40;"OK"&#41;;
		//Create table&#58;
		pspDebugScreenPrintf&#40;"\n"&#41;;
		pspDebugScreenPrintf&#40;"Creating a table..."&#41;;
		sprintf&#40;sql, "create table test_table &#40;column_1 varchar&#40;2&#41;, column_2 numeric&#40;3,0&#41;&#41;;"&#41;;
		retValue = sqlite3_exec&#40;db, sql, SQLiteCallback, 0, &zErr&#41;;
		if &#40;retValue != SQLITE_OK&#41;&#123;
			pspDebugScreenPrintf&#40;"\n"&#41;;
			pspDebugScreenPrintf&#40;"Error creating table&#58; %s", zErr&#41;;
            sqlite3_free&#40;zErr&#41;;
		&#125;else&#123;
            pspDebugScreenPrintf&#40;"OK"&#41;;
			//Insert 100 records&#58;
    		pspDebugScreenPrintf&#40;"\n"&#41;;
            pspDebugScreenPrintf&#40;"Inserting 100 records..."&#41;;
            int i;
            int error = 0;
            for &#40;i=0; i<100; i++&#41;&#123;
                sprintf&#40;sql, "insert into test_table&#40;column_1, column_2&#41; values&#40;'%i', %i&#41;", i, i&#41;;
                retValue = sqlite3_exec&#40;db, sql, SQLiteCallback, 0, &zErr&#41;;
                if &#40;retValue != SQLITE_OK&#41;&#123;
                    pspDebugScreenPrintf&#40;"\n"&#41;;
                    pspDebugScreenPrintf&#40;"Error inserting record %i&#58; %s", i, zErr&#41;;
                    error++;
                &#125;
            &#125;
            if &#40;!error&#41;&#123;
                pspDebugScreenPrintf&#40;"OK"&#41;;
                //Reading 100 records&#58;
                pspDebugScreenPrintf&#40;"\n"&#41;;
                pspDebugScreenPrintf&#40;"Reading 100 records..."&#41;;
                sprintf&#40;sql, "Select * from test_table order by column_1"&#41;;
                retValue = sqlite3_exec&#40;db, sql, SQLiteCallback, 0, &zErr&#41;;
                if &#40;retValue != SQLITE_OK&#41;&#123;
                    pspDebugScreenPrintf&#40;"\n"&#41;;
                    pspDebugScreenPrintf&#40;"Error reading records&#58; %s", zErr&#41;;
                    error++;
                &#125;else&#123;
                    pspDebugScreenPrintf&#40;"OK"&#41;;
                &#125;
            &#125;
		&#125;
	&#125;
	//Close db&#58;
    pspDebugScreenPrintf&#40;"\n"&#41;;
    pspDebugScreenPrintf&#40;"Closing database"&#41;;
	sqlite3_close&#40;db&#41;;

	pspDebugScreenSetXY&#40;0, 27&#41;;
	pspDebugScreenPrintf&#40;"Press X to quit"&#41;;

	SceCtrlData pad;
	while &#40;1&#41;&#123;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if &#40;pad.Buttons & PSP_CTRL_CROSS&#41;&#123;
			break;
		&#125;
	&#125;
	sceKernelExitGame&#40;&#41;;
	return&#40;0&#41;;
&#125;

Code: Select all

TARGET = SQLite
OBJS = main.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;
LIBDIR =

BUILD_PRX=1
LIBS = -lpsppower -lsqlite3
LDFLAGS =
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = SQLite test
PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
Ciaooo
Sakya
bayo
Posts: 4
Joined: Mon Feb 11, 2008 5:04 am
Location: UK

Post by bayo »

Yeahh sakya thanksssss :) I can continue my game now! ^^

It works perfectly hehe

Ciaoo :P
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Post by sakya »

Hi! :)
bayo wrote:Yeahh sakya thanksssss :)
No problem, I had this on my hd. ;)
Just note that if you don't want to use the callback when selecting records you can use this method:

Code: Select all

sqlite3_stmt *stmt;

int retValue = sqlite3_prepare&#40;db, "Select * from tableName", -1, &stmt, 0&#41;;
if &#40;retValue != SQLITE_OK&#41;&#123;
    ERROR!
&#125;

while&#40;sqlite3_step&#40;stmt&#41; == SQLITE_ROW&#41; &#123;
    PROCESS EACH ROW
&#125;
sqlite3_finalize&#40;stmt&#41;;
Ciaooo
Sakya
bayo
Posts: 4
Joined: Mon Feb 11, 2008 5:04 am
Location: UK

Post by bayo »

Ok, it works, but now I need a more difficult thing:

When I select data from a table, I need to have the result in array. Sample:

Code: Select all

Table test_tbl
col1        col2        -> col name
data1      data2     -> data stored
When I select * from test_tbl I want to have this: array[0] = data1 and array[1] = data2. It's possible? :P

PD: Other question hehe: It's needed to have the Select query in a while??? I say this because if I do a select and with the callback it writes the result to the screen, it's only 1 sec in screen, after this, the result disappear. But if I have the select query in a while, the result is always in screen, but it's always reading from MS.

Edit: Solved! ^^

Thanksss
Post Reply