Ikke
2010-11-26 01:10:11 UTC
Hi everybody,
I'm trying to do some basic select/insert/update stuff with an SQLite
database, and I'm using the SQLite 3 unit by Tim Anderson, which I found
on the internet.
Everything works just fine when I pass complete sql statements, but I
cannot seem to get parameters to work. I've tried just about every
possible combination, but nothing seems to work.
Here's a bit of code:
---
function FindByName(name : String) : integer;
var
db : TSQLiteDatabase;
tb : TSQLiteTable;
seq : integer;
query : TSQLiteQuery;
begin
seq := -1;
db := OpenDatabase; // function which returns an TSQLiteDatabase
try
query := db.PrepareSQL('SELECT seq FROM thing WHERE LOWER(name) =
:Name');
db.AddParamText(':Name', name);
tb := db.GetTable(query.SQL);
if (tb.Count > 0) then
begin
seq := tb.FieldAsInteger(0);
end;
except on e : Exception do
seq := -1;
end;
CloseDatabase(db); // basically just FreeAndNil(db);
result := seq;
end;
---
Using db.GetTable (without preparing the SQL) doesn't work either, and I
have no idea what I'm doing wrong :(
Another attempt:
---
query := db.PrepareSQL('INSERT INTO thing (seq, name) VALUES (:Seq,
:Name);');
db.BindSQL(query, 1, seq); // also tried index 0
db.BindSQL(query, 2, name); // also tried index 1
db.ExecSQL(query);
db.ReleaseSQL(query);
---
didn't work either - I've got one insert statement working, it inserted
the seq (an integer value), and the first letter of the name (no idea
why).
Does anybody else use these SQLite 3 units? If so, could you please have
a look at my code and point out the errors? I would be ever so grateful!
Thanks in advance,
Ikke
I'm trying to do some basic select/insert/update stuff with an SQLite
database, and I'm using the SQLite 3 unit by Tim Anderson, which I found
on the internet.
Everything works just fine when I pass complete sql statements, but I
cannot seem to get parameters to work. I've tried just about every
possible combination, but nothing seems to work.
Here's a bit of code:
---
function FindByName(name : String) : integer;
var
db : TSQLiteDatabase;
tb : TSQLiteTable;
seq : integer;
query : TSQLiteQuery;
begin
seq := -1;
db := OpenDatabase; // function which returns an TSQLiteDatabase
try
query := db.PrepareSQL('SELECT seq FROM thing WHERE LOWER(name) =
:Name');
db.AddParamText(':Name', name);
tb := db.GetTable(query.SQL);
if (tb.Count > 0) then
begin
seq := tb.FieldAsInteger(0);
end;
except on e : Exception do
seq := -1;
end;
CloseDatabase(db); // basically just FreeAndNil(db);
result := seq;
end;
---
Using db.GetTable (without preparing the SQL) doesn't work either, and I
have no idea what I'm doing wrong :(
Another attempt:
---
query := db.PrepareSQL('INSERT INTO thing (seq, name) VALUES (:Seq,
:Name);');
db.BindSQL(query, 1, seq); // also tried index 0
db.BindSQL(query, 2, name); // also tried index 1
db.ExecSQL(query);
db.ReleaseSQL(query);
---
didn't work either - I've got one insert statement working, it inserted
the seq (an integer value), and the first letter of the name (no idea
why).
Does anybody else use these SQLite 3 units? If so, could you please have
a look at my code and point out the errors? I would be ever so grateful!
Thanks in advance,
Ikke