0

伙计们,现在我有一个问题,这真的让我想知道。

我目前正在开发一个 Windows 8 RT 应用程序,该应用程序将数据存储到本地,所以我选择使用 SQLite for WinRT(包括 SQLite.cs SQLiteAsync.cs,SQLite3.dll),用于 WinRT 的 SQLite 存储数据库文件默认在应用程序临时文件夹中

public SQLiteConnection (string databasePath, bool storeDateTimeAsTicks = false): this (databasePath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create, storeDateTimeAsTicks)
        {
        }

        /// <summary>
        /// Constructs a new SQLiteConnection and opens a SQLite database specified by databasePath.
        /// </summary>
        /// <param name="databasePath">
        /// Specifies the path to the database file.
        /// </param>
        /// <param name="storeDateTimeAsTicks">
        /// Specifies whether to store DateTime properties as ticks (true) or strings (false). You
        /// absolutely do want to store them as Ticks in all new projects. The default of false is
        /// only here for backwards compatibility. There is a *significant* speed advantage, with no
        /// down sides, when setting storeDateTimeAsTicks = true.
        /// </param>
        public SQLiteConnection (string databasePath, SQLiteOpenFlags openFlags, bool storeDateTimeAsTicks = false)
        {
            DatabasePath = databasePath;

#if NETFX_CORE
            SQLite3.SetDirectory(/*temp directory type*/2, Windows.Storage.ApplicationData.Current.TemporaryFolder.Path);
#endif

            Sqlite3DatabaseHandle handle;

#if SILVERLIGHT || USE_CSHARP_SQLITE
            var r = SQLite3.Open (databasePath, out handle, (int)openFlags, IntPtr.Zero);
#else
            // open using the byte[]
            // in the case where the path may include Unicode
            // force open to using UTF-8 using sqlite3_open_v2
            var databasePathAsBytes = GetNullTerminatedUtf8 (DatabasePath);
            var r = SQLite3.Open (databasePathAsBytes, out handle, (int) openFlags, IntPtr.Zero);
#endif

            Handle = handle;
            if (r != SQLite3.Result.OK) {
                throw SQLiteException.New (r, String.Format ("Could not open database file: {0} ({1})", DatabasePath, r));
            }
            _open = true;

            StoreDateTimeAsTicks = storeDateTimeAsTicks;

            BusyTimeout = TimeSpan.FromSeconds (0.1);
        }

分配给应用程序临时文件夹路径。

现在我想将数据库文件保存到另一个文件夹,如文档文件夹,以保存用户数据和行为,当用户重新安装应用程序时导入数据。所以我更改了保存文件夹,代码如下

StorageFolder sourceFolder = await KnownFolders.DocumentsLibrary.GetFolderAsync(FolderName);
DatabasePath = Path.Combine(sourceFolder.Path, DBName);

SQLite3.SetDirectory(/*temp directory type*/2, storeFloderPath);

但它会在以下位置引发异常:

var r = SQLite3.Open(databasePathAsBytes, out handle, (int)openFlags, IntPtr.Zero);

            Handle = handle;
            if (r != SQLite3.Result.OK)
            {
                throw SQLiteException.New(r, String.Format("Could not open database file: {0} ({1})", DatabasePath, r));
            }

它说无法打开文件。我认为问题可能是'SQLite3.SetDirectory(/ temp directory type /2,storeFloderPath)','2'是stand temp directory type。这些没有官方文档,所以我尝试了从0到6的参数,它不起作用,异常与原始相同。

任何人都知道该怎么做,或者我的代码中有一些错误。提前致谢。

4

1 回答 1

0

谢谢大家的回答,我找到了一个病态的方法来解决它,当我想在其他文件夹中打开数据库连接时,首先将数据库文件复制到应用程序本地文件夹(Windows.Storage.ApplicationData.Current.LocalFolder),然后连接到数据库文件,然后就成功了。

但我希望 WinRT 的 SQLite 的开发人员可以评论这个问题,并修复它

于 2013-08-05T07:45:05.237 回答