UWP C# 中的 SQLite 备份

SQLite Backup in UWP C#

我正在尝试在我的 UWP 应用程序中为 SQLite3 创建数据库备份。基于 SQLite (https://www.sqlite.org/backup.html) 的在线备份 API,我编写了以下方法。 backup_init 正在返回扩展错误代码 7。SQLite 文档中的定义:

(7) SQLITE_NOMEM

The SQLITE_NOMEM result code indicates that SQLite was unable to allocate all >the memory it needed to complete the operation. In other words, an internal >call to sqlite3_malloc() or sqlite3_realloc() has failed in a case where the >memory being allocated was required in order to continue the operation.

我不熟悉 C# 中的指针,我认为我对它们有误。感谢任何帮助。

    public static string BackupDB()
    {
        IntPtr pDb = Marshal.StringToHGlobalUni(Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "DB.sqlite")); //Database to backup
        string zFilename = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "DBBACKUP.sqlite"); //destination db path

        string debug = "";
        IntPtr pFile; //Database connection opened on zFilename
        IntPtr pBackup; //Backup handle used to copy data


        /* Open the database file identified by zFilename. */
        var rc = SQLite3.Open(zFilename, out pFile);
        debug += rc.ToString();

        if (rc == SQLite.Net.Interop.Result.OK)
        {
            /* Open the sqlite3_backup object used to accomplish the transfer */
            pBackup = SQLite3.sqlite3_backup_init(pFile, "main", pDb, "main");

            if (pBackup != null)
            {
                /* Each iteration of this loop copies 5 database pages from database
                ** pDb to the backup database. If the return value of backup_step()
                ** indicates that there are still further pages to copy, sleep for
                ** 250 ms before repeating. */
                do
                {
                    rc = SQLite3.sqlite3_backup_step(pBackup, 5);

                    //xProgress(
                    //    sqlite3_backup_remaining(pBackup),
                    //    sqlite3_backup_pagecount(pBackup)
                    //);
                    if (rc == SQLite.Net.Interop.Result.OK || rc == SQLite.Net.Interop.Result.Busy || rc == SQLite.Net.Interop.Result.Locked)
                    {
                        SQLite3.sqlite3_sleep(250);
                    }
                } while (rc == SQLite.Net.Interop.Result.OK || rc == SQLite.Net.Interop.Result.Busy || rc == SQLite.Net.Interop.Result.Locked);

                /* Release resources allocated by backup_init(). */
                SQLite3.sqlite3_backup_finish(pBackup);
            }

            debug += SQLite3.sqlite3_extended_errcode(pBackup);
        }

        /* Close the database connection opened on database file zFilename
        ** and return the result of this function. */
        SQLite3.Close(pFile);

        return debug;
    }

根据您的代码片段,您正在使用 SQLite.Net-PCL nuget 包。

首先,我无法用上面的代码重现您的问题,我可以初始化并获取 pBackup 对象。

其次,其实你不需要这么复杂的方法来备份uwp应用程序中的SQLite文件。 SQLite 备份有三种方式,其中一种是使用外部工具复制数据库文件。在uwp中,有API可以复制文件。您可以使用 CopyAsync method of StorageFile class 来备份数据库文件。例如:

public static async void BackupDBByCopy()
{
    StorageFolder localfolder = ApplicationData.Current.LocalFolder;        
    StorageFile dboriginal = await localfolder.GetFileAsync("DB.sqlite");         
    await dboriginal.CopyAsync(localfolder, "DBBACKUP.sqlite", NameCollisionOption.ReplaceExisting);
}