delphi FireDAC 下的 Sqlite [6] - 加密


本文整理自网络,侵删。

 
主要就是设置 TFDConnection 的两个链接参数: Password, NewPassword, 非常简单.

const
  dbPath = 'C:\Temp\SQLiteTest.sdb';

{建立加密数据库, 密码是 mm123}
procedure TForm1.FormCreate(Sender: TObject);
const
  strTable = 'CREATE TABLE MyTable(Id integer, Name string(10), Age byte)'; //Id, Name, Age 三个字段
begin
  if FileExists(dbPath) then DeleteFile(dbPath);

  FDConnection1.Params.Add('DriverID=SQLite');
  FDConnection1.Params.Add('Database=' + dbPath);
  FDConnection1.Params.Add('Password=mm123'); //相同与 Password=aes-256:mm123; aes-256 是默认的加密算法;
                                              //还有: aes-128,aes-192,aes-256, aes-ctr-128,aes-ctr-192,aes-ctr-256, aes-ecb-128,aes-ecb-192,aes-ecb-256
  //建表并输入测试数据
  FDConnection1.ExecSQL(strTable);
  FDConnection1.ExecSQL('INSERT INTO MyTable(Id, Name, Age) VALUES(:1, :2, :3)', [1, 'abc', 23]);
end;

{打开有密码的数据库}
procedure TForm1.Button1Click(Sender: TObject);
begin
  FDConnection1.Params.Clear;
  FDConnection1.Connected := False;

  FDConnection1.Params.Add('DriverID=SQLite');
  FDConnection1.Params.Add('Database=' + dbPath);
  FDConnection1.Params.Add('Password=mm123');
  FDConnection1.Connected := True;
  FDQuery1.Open('SELECT * FROM MyTable');
end;

{修改密码}
procedure TForm1.Button2Click(Sender: TObject);
begin
  FDConnection1.Params.Clear;
  FDConnection1.Connected := False;

  FDConnection1.Params.Add('DriverID=SQLite');
  FDConnection1.Params.Add('Database=' + dbPath);
  FDConnection1.Params.Add('Password=mm123');
  FDConnection1.Params.Add('NewPassword=mm12345'); //新密码, 密码为空表示取消密码
  FDConnection1.Connected := True;
  FDConnection1.Connected := False;
end;

FireDAC 还提供了一个专用的加密控件 TFDSQLiteSecurity, 因为上面的方法足够方便了, 所以用处不大.
FDSQLiteSecurity1.DriverLink := FDPhysSQLiteDriverLink1;  //TFDSQLiteSecurity 和 FireDAC.Phys.SQLite 里的其他四个控件, 使用前都要先指定这个属性
FDSQLiteSecurity1.Database := dbPath;    //指定数据库路径
FDSQLiteSecurity1.Password := 'mm111';   //密码
FDSQLiteSecurity1.ToPassword := 'mm222'; //新密码
FDSQLiteSecurity1.SetPassword;           //设置密码
FDSQLiteSecurity1.ChangePassword;        //修改密码
FDSQLiteSecurity1.RemovePassword;        //移除密码

帮助里面说: 通过 FireDAC 加密的 SQLite 与其它并不兼容.

相关阅读 >>

Delphi idftp 错误:socket error 10054 �c connection reset by peer -连接被重置

Delphi stringgrid行列的增加和删除

Delphi 如何判断html编码格式,解决乱码问题

Delphi idhttp数据自动编码

Delphi access violations 问题的解决之道

Delphi-基础(for循环)

Delphi 英文单词第一个字母大写

Delphi mscomm 实时串口通讯

Delphi 编写ie代理服务器

Delphi取整函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...