gpt4 book ai didi

c# - UWP C# 中的 SQLite 备份

转载 作者:行者123 更新时间:2023-12-01 22:18:46 28 4
gpt4 key购买 nike

我正在尝试在我的 UWP 应用程序中为 SQLite3 创建数据库备份。基于 SQLite 的在线备份 API ( https://www.sqlite.org/backup.html ),我编写了以下方法。 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方法StorageFile类来备份数据库文件。例如:

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);
}

关于c# - UWP C# 中的 SQLite 备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42767108/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com