SQLite实现加密

前端之家收集整理的这篇文章主要介绍了SQLite实现加密前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite3 4 Delphi:Delphi中操作sqlite库。项目主页:http://code.google.com/p/sv-utils/最新库下载:http://sv-utils.googlecode.com/files/SQLite3_4_Delphi_RC2.zip

wxsqlite:这是wxWidgets组件之一,我们用它来实现加密。下载地址:http://jaist.dl.sourceforge.net/project/wxcode/Components/wxSQLite3/wxsqlite3-3.0.3.zip


下载完上述两个文件后,分别取两个文件中的部分文件

1,sqlite3 4 Delphi:取“sqlite\sv-utils\sqlite3\Source”目录下3个文件:“sqlite3.pas”、“sqliteTable3.pas”、“sv.inc”,放到工程里,并 把sqlite3.pas”、“sqliteTable3.pas”这两个文件添加到工程里,在使用地方引入这两个单元。

2,wxsqlite:取“wxsqlite3-3.0.3\sqlite3\secure\aes128\dll\release”下的“sqlite3.dll”,放到程序运行目录中。


上面文件准备完毕后,就已经完成对数据库加密需要的全部文件了。


演示代码

unit Unit1;

interface

uses
  Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    logMemo: TMemo;
    clearLogBtn: TButton;
    Button2: TButton;
    dbNameEdt: TEdit;
    Label1: TLabel;
    passwordEdt: TEdit;
    Label2: TLabel;
    createTableBtn: TButton;
    insertDataBtn: TButton;
    deleteDataBtn: TButton;
    selectDataBtn: TButton;
    procedure clearLogBtnClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure createTableBtnClick(Sender: TObject);
    procedure insertDataBtnClick(Sender: TObject);
    procedure selectDataBtnClick(Sender: TObject);
    procedure deleteDataBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure MyLog(tempLog: string); // log方法
  end;

var
  Form2: TForm2;

  logInfo: string; // log信息

implementation

{$R *.dfm}

uses sqlite3,sqliteTable3;

procedure TForm2.Button2Click(Sender: TObject);
var
  sldb: TsqliteDatabase;
  sltb: TsqliteTable;
  ssql: String;
  dbName: string; // 数据库名
  password: string; // 密码
begin

  dbName := trim(dbNameEdt.Text);
  password := trim(passwordEdt.Text);

  try
    sltb := nil;
    sldb := TsqliteDatabase.Create(dbName,seUTF8,password);
    sldb.ChangePassword(password);

    MyLog('加密数据库创建完毕:数据库 = ' + dbName + ' ; 密码 = ' + password);

  finally
    sldb.Free;
    if sltb <> nil then
    begin
      sltb.Free;
    end;
  end;

end;

procedure TForm2.clearLogBtnClick(Sender: TObject);
begin
  logMemo.Clear;
end;

// 创建表
procedure TForm2.createTableBtnClick(Sender: TObject);
var
  sldb: TsqliteDatabase;
  sltb: TsqliteTable;
  ssql: String;
  dbName: string; // 数据库名
  password: string; // 密码
begin

  dbName := trim(dbNameEdt.Text);
  password := trim(passwordEdt.Text);

  try
    sltb := nil;
    sldb := TsqliteDatabase.Create(dbName,password);
    sldb.ChangePassword(password);

    ssql := 'CREATE TABLE IF NOT EXISTS [test] (';
    ssql := ssql + '[a] VARCHAR(100)  NULL,';
    ssql := ssql + '[b] VARCHAR(100)  NULL';
    ssql := ssql + ')';

    sldb.Execsql(ssql);

    MyLog('创建表: test');

  finally
    sldb.Free;
    if sltb <> nil then
    begin
      sltb.Free;
    end;
  end;
end;

procedure TForm2.deleteDataBtnClick(Sender: TObject);
var
  sldb: TsqliteDatabase;
  sltb: TsqliteTable;
  ssql: String;
  dbName: string; // 数据库名
  password: string; // 密码
begin

  dbName := trim(dbNameEdt.Text);
  password := trim(passwordEdt.Text);

  try
    sltb := nil;
    sldb := TsqliteDatabase.Create(dbName,password);
    sldb.ChangePassword(password);

    ssql := 'DELETE FROM [test] ';
    sldb.Execsql(ssql);

    MyLog('删除数据完毕 ');

  finally
    sldb.Free;
    if sltb <> nil then
    begin
      sltb.Free;
    end;
  end;
end;

// 添加数据
procedure TForm2.insertDataBtnClick(Sender: TObject);
var
  sldb: TsqliteDatabase;
  sltb: TsqliteTable;
  ssql: String;
  dbName: string; // 数据库名
  password: string; // 密码
  i: integer;
  maxCount: integer; // 添加数据最大数量
begin

  dbName := trim(dbNameEdt.Text);
  password := trim(passwordEdt.Text);

  try
    sltb := nil;
    sldb := TsqliteDatabase.Create(dbName,password);
    sldb.ChangePassword(password);

    maxCount := 9;

    for i := 0 to maxCount do
    begin
      ssql := 'INSERT INTO [test] VALUES (';
      ssql := ssql + QuotedStr('a' + inttostr(i)) + ',';
      ssql := ssql + QuotedStr('b' + inttostr(i));
      ssql := ssql + ')';

      sldb.Execsql(ssql);
    end;

    MyLog('共添加: ' + inttostr(maxCount + 1) + ' 条数据');

  finally
    sldb.Free;
    if sltb <> nil then
    begin
      sltb.Free;
    end;
  end;
end;

// log方法
procedure TForm2.MyLog(tempLog: string);
var
  temp: string;
  oldLog: string;
begin
  if trim(tempLog) <> '' then
  begin

    oldLog := trim(logMemo.Text);
    logMemo.Clear;

    temp := FormatDateTime('yyyy-mm-dd hh:mm:ss',now) + ' ' + trim(tempLog);
    if oldLog = '' then
    begin
      logMemo.Lines.Add(temp);
      logMemo.Lines.Add('');
    end
    else
    begin
      logMemo.Lines.Add(temp);
      logMemo.Lines.Add('');
      logMemo.Lines.Add(oldLog);
    end;

  end;

end;

procedure TForm2.selectDataBtnClick(Sender: TObject);
var
  sldb: TsqliteDatabase;
  sltb: TsqliteTable;
  ssql: String;
  dbName: string; // 数据库名
  password: string; // 密码
  totalCount: integer;
  temp: string;
begin

  dbName := trim(dbNameEdt.Text);
  password := trim(passwordEdt.Text);

  try
    sltb := nil;
    sldb := TsqliteDatabase.Create(dbName,password);
    sldb.ChangePassword(password);

    ssql := 'SELECT * FROM [test] ';
    sltb := sldb.GetTable(ssql);

    // 1,显示总数据条数

    totalCount := 0;
    if not sltb.EOF then
    begin
      totalCount := sltb.Count;
    end;

    MyLog('共: ' + inttostr(totalCount) + ' 条数据');

    // 2,显示详细信息

    temp := '';

    while not sltb.EOF do
    begin

      if temp = '' then
      begin
        temp := #13#10 + sltb.FieldValByNameAsString['a'] + ' ; ' +
          sltb.FieldValByNameAsString['b'] + #13#10;
      end
      else
      begin
        temp := temp + sltb.FieldValByNameAsString['a'] + ' ; ' +
          sltb.FieldValByNameAsString['b'] + #13#10;
      end;

      sltb.Next;
    end;

    if temp <> '' then
    begin
      MyLog('详细数据:' + #13#10 + temp);
    end;

  finally
    sldb.Free;
    if sltb <> nil then
    begin
      sltb.Free;
    end;
  end;
end;

end.
原文链接:https://www.f2er.com/sqlite/201284.html

猜你在找的Sqlite相关文章