如何在Xamarin应用程序中设置SQLite.NET页面大小? PRAGMA page_size被忽略

时间:2015-02-20 23:33:50

标签: xamarin xamarin.forms sqlite.net

经过大量搜索后,我无法找到如何从默认的4096更改SQLite.NET Pagesize。我有一个Xamarin Forms PCL应用程序与SQLite.NET Async PCL Nuget(和Twincoders Extensions但是那是不相关)。

在平台项目中,SQLiteConnectionString似乎没有设置Pagesize的工具。

我的iOS代码如下所示:

private static SQLiteConnectionString connectionString = new SQLiteConnectionString(Path.Combine(DatabaseFilePath, DatabaseFilename), false);
private static SQLiteAsyncConnection asyncConnection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(new SQLitePlatformIOS(), connectionString));

public SQLiteAsyncConnection GetAsyncConnection() 
{
    return asyncConnection;
}

帮助表示感谢!如果没有办法做到这一点,那么也应该知道这一点。

********编辑:********

@Paul建议使用PRAGMA page_size,我确信这是正确的想法。但是,SQLite.Net忽略了更改page_size的请求,即使尚未创建任何表并且日志记录模式为DEFAULT。至少在iOS中。除非我的代码错误:

在Xamarin Forms PCL中我有:

public interface ISQLite
{
    SQLiteAsyncConnection GetAsyncConnection ();
}

在我的iOS项目中,我有:

private static string DatabaseFilename = "MyDatabase.db3";

private static string DatabaseFilePath
{
    get
    {
        return (NSFileManager.DefaultManager.GetUrls
                    (NSSearchPathDirectory.LibraryDirectory,
                        NSSearchPathDomain.User) [0]).ToString();
    }
}

private static readonly SQLiteConnectionString connectionString = 
    new SQLiteConnectionString(Path.Combine(DatabaseFilePath, DatabaseFilename), false);
private static readonly SQLiteAsyncConnection asyncConnection = 
    new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(new SQLitePlatformIOS(), connectionString));

public SQLiteAsyncConnection GetAsyncConnection() 
{
    var syncTask = new Task (() =>
    {
        //asyncConnection.ExecuteAsync("PRAGMA journal_mode=DELETE");
        asyncConnection.ExecuteAsync ("PRAGMA page_size=1024"); // Has no effect on page size
    });
    syncTask.RunSynchronously();
    return asyncConnection;
}

执行时没有错误,但不会更改页面大小。

欢迎所有想法!

********进一步编辑:********

添加VACUUM也没有效果:

asyncConnection.ExecuteAsync ("PRAGMA page_size=1024; vacuum");

1 个答案:

答案 0 :(得分:1)

使用此处描述的pragma语句:

http://www.sqlite.org/pragma.html#pragma_page_size

编辑:这可能是iOS特定的问题。深入挖掘我发现: http://www.sqlite.org/wal.html 在进入WAL模式后,无法在空数据库上或使用VACUUM或使用备份API从备份还原时更改数据库页面大小。您必须处于回滚日志模式才能更改页面大小。

在这里,看起来你可能处于WAL模式: https://developer.apple.com/library/ios/qa/qa1809/_index.html ... Core Data SQLite存储的默认日记模式已更改为iOS 7和OS X Mavericks中的预写日志(WAL)。

您可以尝试" PRAGMA journal_mode = DELETE;"在设置页面大小之前。