SQLITE数据库备份并在Delphi中还原



使用Delphi 10.2 Firemonky Firefac在Android中备份和还原SQLite数据库有什么办法。我正在使用以下代码,但没有生成任何备份文件,我想将备份文件保存在sdcard中(当前尝试保存在当前目录中);

procedure TFMain.Button1Click(Sender: TObject);
var pathname : string;
begin
{$IFDEF ANDROID}
pathname := System.IOUtils.TPath.GetDocumentsPath;
{$ENDIF}
FDConnection1.DriverName := 'SQLite';
FDConnectionMain.Params.Values['Database'] := TPath.Combine(TPath.GetDocumentsPath, 'ConceptDB_Backup.s3db');
FDConnection1.Open();
    FDSQLiteBackup1.Database := System.IOUtils.TPath.Combine(pathname, 'ConceptDB.s3db');
    FDSQLiteBackup1.DestDatabaseObj := FDConnection1.CliObj;
    FDSQLiteBackup1.Backup;
    ShowMessage('Backup Successfully');
end;

如何备份sqlite数据库?

您的代码中有错字。您已经为其他对象定义了数据库名称。我不知道这些对象的含义,但是如果您要备份打开的数据库(主要备份API主要是为了),则可以将其定义为名称定义的文件,那么您可以像这样编写代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  BackupPath: string;
  BackupFile: string;
begin
  BackupPath := System.IOUtils.TPath.GetDocumentsPath;
  BackupFile := System.IOUtils.TPath.Combine(BackupPath, 'MyBackup.db');
  FDSQLiteBackup1.DatabaseObj := FDConnection1.CliObj as TSQLiteDatabase;
  FDSQLiteBackup1.DestDatabase := BackupFile;
  FDSQLiteBackup1.Backup;
end;

或通过文件名来定义源和目标数据库:

procedure TForm1.Button1Click(Sender: TObject);
var
  CommonPath: string;
  SourceFile: string;
  BackupFile: string;
begin
  CommonPath := System.IOUtils.TPath.GetDocumentsPath;
  SourceFile := System.IOUtils.TPath.Combine(CommonPath, 'MySource.db');
  BackupFile := System.IOUtils.TPath.Combine(CommonPath, 'MyBackup.db');
  FDSQLiteBackup1.Database := SourceFile;
  FDSQLiteBackup1.DestDatabase := BackupFile;
  FDSQLiteBackup1.Backup;
end;

整个过程是关于2个属性和一个方法调用。您始终指定源对象或源文件名,以及目标对象或目标文件名。

如何还原SQLite数据库?

sqlite backup api 创建的备份文件(> tfdsqlitebackup component使用)只是数据库副本可以以与原始方式相同的方式打开。

如何将文件写入外部存储?

如果您在启用项目设置中保留写入外部存储选项,则使用外部存储(如果这是SD卡的含义),则可能会完成数据ATATABASE备份文件,并且用于备份文件构建一条路径,例如通过使用此代码(它实现了 getExternalStoreDirectory 方法)。您的代码可能会变成(未经测试):

uses
  Androidapi.Helpers,
  Androidapi.JNIBridge,
  Androidapi.JNI.JavaTypes;
type
  JEnvironment = interface;
  JEnvironmentClass = interface(JObjectClass)
  ['{D131F4D4-A6AD-43B7-B2B6-A9222BC46C74}']
    function _GetMEDIA_BAD_REMOVAL: JString; cdecl;
    function _GetMEDIA_CHECKING: JString; cdecl;
    function _GetMEDIA_EJECTING: JString; cdecl;
    function _GetMEDIA_MOUNTED: JString; cdecl;
    function _GetMEDIA_MOUNTED_READ_ONLY: JString; cdecl;
    function _GetMEDIA_NOFS: JString; cdecl;
    function _GetMEDIA_REMOVED: JString; cdecl;
    function _GetMEDIA_SHARED: JString; cdecl;
    function _GetMEDIA_UNKNOWN: JString; cdecl;
    function _GetMEDIA_UNMOUNTABLE: JString; cdecl;
    function _GetMEDIA_UNMOUNTED: JString; cdecl;
    function _GetDIRECTORY_ALARMS: JString; cdecl;
    function _GetDIRECTORY_DCIM: JString; cdecl;
    function _GetDIRECTORY_DOCUMENTS: JString; cdecl;
    function _GetDIRECTORY_DOWNLOADS: JString;
    function _GetDIRECTORY_MOVIES: JString; cdecl;
    function _GetDIRECTORY_MUSIC: JString; cdecl;
    function _GetDIRECTORY_NOTIFICATIONS: JString; cdecl;
    function _GetDIRECTORY_PICTURES: JString; cdecl;
    function _GetDIRECTORY_PODCASTS: JString; cdecl;
    function _GetDIRECTORY_RINGTONES: JString; cdecl;
    {class} function init: JEnvironment; cdecl;
    {class} function getDataDirectory: JFile; cdecl; 
    {class} function getDownloadCacheDirectory: JFile; cdecl;
    {class} function getExternalStorageDirectory(): JFile; cdecl;
    {class} function getExternalStoragePublicDirectory(type: JString): JFile; cdecl;
    {class} function getExternalStorageState(path: JFile): JString; cdecl; 
    {class} function getExternalStorageState: JString; cdecl;
    {class} function getRootDirectory: JFile; cdecl;
    {class} function getStorageState(path: JFile): JString; cdecl; 
    {class} function isExternalStorageEmulated: Boolean; cdecl;
    {class} function isExternalStorageEmulated(path: JFile): Boolean; cdecl;
    {class} function isExternalStorageRemovable(path: JFile): Boolean; cdecl;
    {class} function isExternalStorageRemovable: Boolean; cdecl;
    {class} property MEDIA_BAD_REMOVAL: JString read _GetMEDIA_BAD_REMOVAL;
    {class} property MEDIA_CHECKING: JString read _GetMEDIA_CHECKING;
    {class} property MEDIA_EJECTING: JString read _GetMEDIA_EJECTING;
    {class} property MEDIA_MOUNTED: JString read _GetMEDIA_MOUNTED;
    {class} property MEDIA_MOUNTED_READ_ONLY: JString read _GetMEDIA_MOUNTED_READ_ONLY;
    {class} property MEDIA_NOFS: JString read _GetMEDIA_NOFS;
    {class} property MEDIA_REMOVED: JString read _GetMEDIA_REMOVED;
    {class} property MEDIA_SHARED: JString read _GetMEDIA_SHARED;
    {class} property MEDIA_UNKNOWN: JString read _GetMEDIA_UNKNOWN;
    {class} property MEDIA_UNMOUNTABLE: JString read _GetMEDIA_UNMOUNTABLE;
    {class} property MEDIA_UNMOUNTED: JString read _GetMEDIA_UNMOUNTED;
    {class} property DIRECTORY_ALARMS: JString read _GetDIRECTORY_ALARMS;
    {class} property DIRECTORY_DCIM: JString read _GetDIRECTORY_DCIM;
    {class} property DIRECTORY_DOCUMENTS: JString read _GetDIRECTORY_DOCUMENTS;
    {class} property DIRECTORY_DOWNLOADS: JString read _GetDIRECTORY_DOWNLOADS;
    {class} property DIRECTORY_MOVIES: JString read _GetDIRECTORY_MOVIES;
    {class} property DIRECTORY_MUSIC: JString read _GetDIRECTORY_MUSIC;
    {class} property DIRECTORY_NOTIFICATIONS: JString read _GetDIRECTORY_NOTIFICATIONS;
    {class} property DIRECTORY_PICTURES: JString read _GetDIRECTORY_PICTURES;
    {class} property DIRECTORY_PODCASTS: JString read _GetDIRECTORY_PODCASTS;
    {class} property DIRECTORY_RINGTONES: JString read _GetDIRECTORY_RINGTONES;
  end;
  [JavaSignature('android/os/Environment')]
  JEnvironment = interface(JObject)
  ['{83A2E94E-7D8E-432F-BE21-AEC2115015BE}']
  end;
  TJEnvironment = class(TJavaGenericImport<JEnvironmentClass, JEnvironment>);
function GetExtStgPath: string;
var
  Path: JFile;
begin
  Path := TJEnvironment.JavaClass.getExternalStorageDirectory;
  Result := JStringToString(Path.getAbsolutePath);
end;
function IsExtStgWritable: Boolean;
var
  State: JString;
begin
  State := TJEnvironment.JavaClass.getExternalStorageState;
  Result := State.equals(TJEnvironment.JavaClass.MEDIA_MOUNTED);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  SourcePath: string;
  SourceFile: string;
  BackupPath: string;
  BackupFile: string;
begin
  if not IsExtStgWritable then
    raise Exception.Create('The external storage is not writable!');
  SourcePath := System.IOUtils.TPath.GetDocumentsPath;
  SourceFile := System.IOUtils.TPath.Combine(SourcePath, 'MySource.db');
  BackupPath := GetExtStgPath;
  BackupFile := System.IOUtils.TPath.Combine(BackupPath, 'MyBackup.db');
  FDSQLiteBackup1.Database := SourceFile;
  FDSQLiteBackup1.DestDatabase := BackupFile;
  FDSQLiteBackup1.Backup;
end;

最新更新