在 iOS 應用程式部屬到設備上時有個很嚴重的限制:
就是在發佈應用程式時不能附帶任何的 libraries (*.dylib) 。
難道在資料庫的存取上就只能走 DataSnap 嗎?
當然不是,我們還可以使用 iOS 內部支援的 SQLite 當作本機資料庫使用,現在就來介紹新的工具吧 -- LiteDAC!
Devart 的 DAC 元件群(ODAC、MyDAC、PgDAC、IBDAC、UniDAC 和 LiteDAC)有個最大的特色,就是它不需要任何的 libraries (*.dylib),就可以直接存取資料庫。
今天就初步來介紹本次的主角 -- LiteDAC!
使用上非常的輕鬆,底下是原始碼:
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 | procedure TForm1.FormCreate(Sender: TObject);var _Count: Integer; _Temp: TLiteQuery;begin _Temp := TLiteQuery.Create(Self); _Temp.Connection := LiteConnection1; LiteConnection1.Options.ForceCreateDatabase := True; LiteConnection1.Database := IncludeTrailingPathDelimiter(TPath.GetDocumentsPath) + 'emps.sqlite'; LiteConnection1.Connect(); _Temp.SQL.Text := 'SELECT count(*) FROM sqlite_master WHERE type=''table'' AND name=''DEPT'' '; _Temp.Open(); _Count := _Temp.Fields[0].AsInteger; _Temp.Close(); if _Count = 0 then begin _Temp.SQL.Text := '' +'CREATE TABLE DEPT ' +'( ' +' DEPTNO INTEGER NOT NULL, ' +' DNAME VARCHAR(14), ' +' LOC VARCHAR(13), ' +' PRIMARY KEY (DEPTNO) ' +') '; _Temp.ExecSQL(); _Temp.SQL.Text := '' +'INSERT INTO DEPT ' +'VALUES (10, ' +' ''ACCOUNTING'', ' +' ''NEW YORK''); ' +' '; _Temp.ExecSQL(); _Temp.SQL.Text := '' +'INSERT INTO DEPT ' +'VALUES (20, ' +' ''RESEARCH'', ' +' ''DALLAS''); '; _Temp.ExecSQL(); _Temp.SQL.Text := '' +'INSERT INTO DEPT ' +'VALUES (30, ' +' ''SALES'', ' +' ''CHICAGO''); '; _Temp.ExecSQL(); _Temp.SQL.Text := '' +'INSERT INTO DEPT ' +'VALUES (40, ' +' ''OPERATIONS'', ' +' ''BOSTON''); '; _Temp.ExecSQL(); end; LiteQuery1.Open();end; |
![]() |
| 很快速的做出了新的成品 |
1 | LiteConnection1.Options.ForceCreateDatabase := True; |
1 | LiteConnection1.Database := IncludeTrailingPathDelimiter(TPath.GetDocumentsPath) + 'emps.sqlite'; |
附帶說明:LiteDAC 在 iOS 上會智能載入系統自帶的 SQLite 驅動,所以在 LiteConnection.Options.Direct 一定要為 False 。
LiteDAC 官網在這兒

沒有留言:
張貼留言