K
Kevin Proctor
I first found out about the Ruby/DL library on WhyTheLuckyStiff.net in
an article on 1.8 ->
http://whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh It's a
great article and I really liked the idea of using DL to make a pure
ruby extension type deal.
I immediately decided to try to jam it and the SQLite library
(www.sqlite.org) together. Please note - this is my first shot at it,
and I have been drinking and watching LOTR at the same time. So if
there's something really obviously wrong or silly here - that's my
excuse.
That and I had some trouble finding docs for it - but that's
quite possibly my being impatient and wanting to just jump right in.
First I look at the simplest possible version of the SQLite interface.
# typedef struct sqlite sqlite;
# sqlite *sqlite_open(const char *dbname, int mode, char **errmsg);
# int sqlite_exec(
# sqlite *db,
# char *sql,
# int (*xCallback)(void*,int,char**,char**),
# void *pArg,
# char **errmsg
# );
# void sqlite_close(sqlite *db);
So I start off with this in Ruby (less some unimportant stuff)
SqliteStruct = struct []
extern "void *sqlite_open(char*, int, char **)"
extern "void sqlite_close(void*)"
extern "int sqlite_exec(void*, char*, int, void*, char **)"
Now I'm ready to play with my Ruby SQLite interface
dB = Sqlite::SqliteStruct.new(Sqlite.sqlite_open( "temp.db", 0, errMsg))
Sqlite.sqlite_exec(dB, "create table tbl1(one varchar(10), two
smallint)", 0, nil, nil)
Sqlite.sqlite_exec(dB, "insert into tbl1 values('hello!',10)",0, nil, nil)
Sqlite.sqlite_exec(dB, "insert into tbl1 values('goodbye', 20)", 0, nil,
nil)
foo = Sqlite.sqlite_exec(dB, "select * from tbl1", 0, nil, nil)
Sqlite.sqlite_close(dB);
This sucessfully creates the database, puts some stuff in it and then
closes it.
But, and there's always a but, you may have noticed the ugliness in my
sqlite_exec function. I'm just passing an int (0 specifically) - I
really want to put a 'function pointer' there to do neat things with the
results of my sql statement. For the life of me I can not find an
example of doing something like this in DL. I'm sure it's there
somewhere but I'm stumped.
I've decided to ask here if you can see where I've gone wrong.
And to ask if anyone out there has any nead Ruby/DL projects they've
completed.
Kevin
an article on 1.8 ->
http://whytheluckystiff.net/articles/2003/08/04/rubyOneEightOh It's a
great article and I really liked the idea of using DL to make a pure
ruby extension type deal.
I immediately decided to try to jam it and the SQLite library
(www.sqlite.org) together. Please note - this is my first shot at it,
and I have been drinking and watching LOTR at the same time. So if
there's something really obviously wrong or silly here - that's my
excuse.
quite possibly my being impatient and wanting to just jump right in.
First I look at the simplest possible version of the SQLite interface.
# typedef struct sqlite sqlite;
# sqlite *sqlite_open(const char *dbname, int mode, char **errmsg);
# int sqlite_exec(
# sqlite *db,
# char *sql,
# int (*xCallback)(void*,int,char**,char**),
# void *pArg,
# char **errmsg
# );
# void sqlite_close(sqlite *db);
So I start off with this in Ruby (less some unimportant stuff)
SqliteStruct = struct []
extern "void *sqlite_open(char*, int, char **)"
extern "void sqlite_close(void*)"
extern "int sqlite_exec(void*, char*, int, void*, char **)"
Now I'm ready to play with my Ruby SQLite interface
dB = Sqlite::SqliteStruct.new(Sqlite.sqlite_open( "temp.db", 0, errMsg))
Sqlite.sqlite_exec(dB, "create table tbl1(one varchar(10), two
smallint)", 0, nil, nil)
Sqlite.sqlite_exec(dB, "insert into tbl1 values('hello!',10)",0, nil, nil)
Sqlite.sqlite_exec(dB, "insert into tbl1 values('goodbye', 20)", 0, nil,
nil)
foo = Sqlite.sqlite_exec(dB, "select * from tbl1", 0, nil, nil)
Sqlite.sqlite_close(dB);
This sucessfully creates the database, puts some stuff in it and then
closes it.
But, and there's always a but, you may have noticed the ugliness in my
sqlite_exec function. I'm just passing an int (0 specifically) - I
really want to put a 'function pointer' there to do neat things with the
results of my sql statement. For the life of me I can not find an
example of doing something like this in DL. I'm sure it's there
somewhere but I'm stumped.
I've decided to ask here if you can see where I've gone wrong.
And to ask if anyone out there has any nead Ruby/DL projects they've
completed.
Kevin