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,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 与其它并不兼容. 原文链接:https://www.f2er.com/sqlite/199554.html