ZEOS DBO 连接 SQLIte

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值