ZEOS DBO 连接 SQLIte

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, SynDBZeos, ZDbcSqLite, ZDbcUtils, ZAbstractConnection,
  ZConnection, ZAbstractDataset, ZDataset, ZAbstractRODataset, ZCompatibility,
  Grids, DBGrids, DB;

type
  TForm1 = class(TForm)
    btn1: TButton;
    zqry1: TZQuery;
    ds1: TDataSource;
    dbgrd1: TDBGrid;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function SQLiteEncrypted(Filename: string): Boolean;
var
  Header: array[0..14] of byte;
  sHeader: string;
  SQLiteFile: file;
  i: Integer;
begin
  Result := False;
  if FileExists(Filename) then
  begin
    AssignFile(SQLiteFile, Filename);
    Reset(SQLiteFile, 1);
    try
      BlockRead(SQLiteFile, Header, 15);
      for i := 0 to 14 do
        sHeader := sHeader + Chr(Header[i]);
      Result := not (sHeader = 'SQLite format 3');
    finally
      CloseFile(SQLiteFile);
    end;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  ZConnection1: TZconnection;
  i: Integer;
  db: Pointer;
  s, pwd: string;
begin
  ZConnection1 := TZconnection.Create(Self);
  ZConnection1.Protocol := 'sqlite-3';  //非常重要

  if not FileExists('SysDB.db') then
  begin
    Application.MessageBox('database file do not exists', PChar(Application.Title), MB_OK or MB_ICONERROR);
    Application.Terminate;
  end;

  ZConnection1.ClientCodepage := 'utf8';
  ZConnection1.ControlsCodePage := cGET_ACP;
  ZConnection1.AutoEncodeStrings := true;

  ZConnection1.Database := 'SysDB.db';
  ZConnection1.Connect;

  if SQLiteEncrypted('SysDB.db') then
  begin
    s := ZConnection1.Version;
    db := (ZConnection1.DbcConnection as IZSQLiteConnection).GetConnectionHandle;
    i := (ZConnection1.DbcConnection as IZSQLiteConnection).GetPlainDriver.Key(db, PChar(pwd), Length(pwd + s));
    if i <> 0 then
    begin
      //Application.Messagebox('Can''t open encrypted database',PChar(Application.Title),MB_OK OR MB_ICONERROR);
      Application.Terminate;
    end;
    pwd := '';
  end;

  zqry1.Connection := ZConnection1;
  zqry1.SQL.Text := 'select * from testtable';
  zqry1.Open;
end;

end.

转载于:https://my.oschina.net/u/582827/blog/1620448

你可能感兴趣的:(ZEOS DBO 连接 SQLIte)