Rgd.Sqlite3
1.0.0
델파이 용 RGD.SQLITE3- 가벼우면서 간단하고 효과적인 SQLITE3 인터페이스 장치
sqlite3.dll을 동적으로 또는 sqlite3 라이브러리를 정적으로 연결할 수 있습니다.
암호화의 경우 rgd.sqlite3fde.pas를 사용하여 SQLITE의 FIREDAC 암호화 버전 및 액세스 FIREDAC 암호화 데이터베이스를 정적으로 연결하십시오.
크레딧 :
이 단원은 https://github.com/plashenkov/sqlite3-delphi-fpc에서 Yury Plashenkov로부터 아이디어를 빌려줍니다. Yuri의 개념을 염두에두고 Rgd.sqlite3 for Delphi는 인터페이스 된 객체와 익명 방법을 사용하여 구현됩니다. Nick Hodges의 "Delphi in Delphi"를 읽는 아이디어와 데이터를 바인딩하고 가져 오기위한 유연한 음식, 거래 수행을 수행하는 아이디어.
쿼리 패턴 :
{Example Pattern 1 - Stmt := DB.Prepare() and while Stmt.Step...}
var
Stmt: ISqlite3Statement;
begin
Stmt := DB.Prepare(
'SELECT Name,' +
' ID' +
' FROM Tasks');
while Stmt.Step = SQLITE_ROW do
being
S0 := Stmt.SqlColumn[0].AsText;
ID := Stmt.SqlColumn[1].AsInt;
{...}
end);
end;
{Pattern 2 - with DB.Prepare and Fetch(procedure)...}
with DB.Prepare(
'SELECT Name,' +
' ID' +
' FROM Tasks') do Fetch(procedure
begin
S0 := SqlColumn[0].AsText;
ID := SqlColumn[1].AsInt;
{...}
end);
{Pattern 3 - DB.Fetch(SQL, procedure(const Stmt: ISQlite3Statement)...}
DB.Fetch(
'SELECT Name,' +
' ID' +
' FROM Tasks', procedure(const Stmt: ISQlite3Statement)
begin
S0 := Stmt.SqlColumn[0].AsText;
ID := Stmt.SqlColumn[1].AsInt;
{...}
end;
데이터베이스 패턴 만들기 ...
procedure TMainForm.CreateDatabase;
begin
DB := TSqlite3Database.Create;
DB.Open(':memory:');
{Create Table...}
DB.Execute(
' CREATE TABLE Organizations ( ' +
' Name TEXT,' +
' Website TEXT,' +
' Country TEXT,' +
' Description TEXT,' +
' Founded TEXT,' +
' Industry TEXT,' +
' EmployeeCount INTEGER,' +
' PRIMARY KEY (Name ASC))' +
' WITHOUT ROWID');
Stmt_Description := DB.Prepare(
'SELECT Description' +
' FROM Organizations' +
' WHERE Name = ?');
end;
예 : CSV 파일에서 레코드 삽입 ...
procedure TMainForm.ReadCsvIntoDatabase;
var
Lines, Values: TStringlist;
begin
Lines := TStringlist.Create;
Values := TStringlist.Create;
Values.StricDelimiter := True;
try
CreateDatabase;
Lines.LoadFromFile('organizations-1000.csv');
Lines.Delete(0); {Ignore Header}
DB.Transaction(procedure
begin
with DB.Prepare('INSERT INTO Organizations VALUES (?, ?, ?, ?, ?, ?, ?, ?)') do
begin
for var S in Lines do
begin
Values.CommaText := S;
Values.Delete(0); {Ignore first column in our sample .csv}
BindAndStep(Values.ToStringArray);
end;
end;
end);
finally
Values.Free;
Lines.Free;
end;
DB.Execute('ANALYZE');
end;
예 : 응용 프로그램 정의 기능
procedure SqlAdf_SizeCategory(Context: Pointer; n: integer; args: PPSQLite3ValueArray); cdecl;
var
Count: integer;
Result: string;
begin
Count := TSqlite3.ValueInt(Args[0]);
case Count of
0..500: Result := 'Small';
501..5000: Result := 'Medium';
5001..MaxInt: Result := 'Large';
end;
TSqlite3.ResultText(Context, Result);
end;
{...}
DB.CreateFunction('SizeCategory', 1, @SqlAdf_SizeCategory);
{...}
with DB.Prepare(
'SELECT OrgID, Name, Website, Country, Industry, Founded, EmployeeCount, SizeCategory(EmployeeCount) as SizeCat' +
' FROM Organizations' +
' WHERE Country LIKE ?' +
' AND SizeCat LIKE ?' +
' ORDER BY 2') do BindAndFetch([FCountry, FSizeCat], procedure
begin
{...}
end