1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
program lazarustest;
uses
SQLiteTable3 in 'SQLiteTable3.pas',
SQLite3 in 'SQLite3.pas',
sysutils;
procedure DoTest();
var
slDBpath : string;
sldb : TSQLiteDatabase;
sltb : TSQLIteTable;
sSQL : String;
Notes : String;
begin
// needed for linux build.
slDBPath := ExtractFilepath( paramstr(0) ) + 'test.db';
sldb := TSQLiteDatabase.Create(slDBPath);
try
if sldb.TableExists('testTable') then
begin
sSQL := 'DROP TABLE testtable';
sldb.execsql(sSQL);
end;
sSQL := 'CREATE TABLE testtable ([ID] INTEGER PRIMARY KEY,[OtherID] INTEGER NULL,';
sSQL := sSQL + '[Name] VARCHAR (255),[Number] FLOAT, [notes] BLOB, [picture] BLOB COLLATE NOCASE);';
sldb.execsql(sSQL);
sldb.execsql('CREATE INDEX TestTableName ON [testtable]([Name]);');
//begin a transaction
sldb.BeginTransaction;
sSQL := 'INSERT INTO testtable(Name,OtherID,Number,Notes) VALUES ("Some Name",4,587.6594,"Here are some notes");';
//do the insert
sldb.ExecSQL(sSQL);
sSQL := 'INSERT INTO testtable(Name,OtherID,Number,Notes) VALUES ("Another Name",12,4758.3265,"More notes");';
//do the insert
sldb.ExecSQL(sSQL);
//end the transaction
sldb.Commit;
//query the data
sltb := slDb.GetTable('SELECT * FROM testtable');
try
if sltb.Count > 0 then
begin
//display first row
writeln( sltb.FieldAsString(sltb.FieldIndex['Name']) );
writeln( inttostr(sltb.FieldAsInteger(sltb.FieldIndex['ID'])) );
writeln( floattostr( sltb.FieldAsDouble(sltb.FieldIndex['Number'])) );
end;
finally
sltb.Free;
end;
finally
sldb.Free;
end;
end;
begin
try
DoTest();
writeln( 'SqlLite3 unit IS lazarus compatible' );
except
writeln( 'ERROR : SqlLite3 unit is NOT lazarus compatible' );
end;
end.
|