1

I'm trying to make a preloader C# program for a sqlite database. When I create then read the file, I get exeptions. When I delete and make a ew one, I get exceptions. Can someone please let me know what I'm doing wrong?!

string dbPath = @"..\..\..\TidesDatabase\Assets\Database.3db";
        bool exists = File.Exists (dbPath);
        if ( exists )
        {
            File.SetAttributes( dbPath, FileAttributes.Normal );
            File.Delete( dbPath );
            File.Create( dbPath );
            File.SetAttributes( dbPath, FileAttributes.Normal );
        }
        else
        {
            File.Create( dbPath );
            File.SetAttributes( dbPath, FileAttributes.Normal );
        }
        var db = new SQLiteConnection( dbPath );

The last line is where the exception is thrown.

Stacktrace:

at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks) in c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\Preloader\SqliteNet.cs:line 153 at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks) in c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\Preloader\SqliteNet.cs:line 114 at Preloader.Program.Main(String[] args) in c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\Preloader\Program.cs:line 40 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

4

2 回答 2

0

注意:我假设有问题的代码是 1) 在控制台应用程序中运行,而不是在移动操作系统上运行。2)使用 SQLiteNET(一个 ORM)而不是直接使用 SQLite。

问题似乎是您正在创建一个空文件,然后期望 sqlite 将其作为数据库文件打开。如果数据库文件不存在,则需要让 sqlite 创建它。这是一个例子:

string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Database.db3");

// If a db file doesn't exist, SQLite-Net will create it
var db = new SQLiteConnection (dbPath);
于 2013-12-10T22:54:53.390 回答
0

我认为您没有从这样的路径获取文件的正确权限。

要获取文件,您应该执行以下操作:

string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),
    "Database.3db");

如果您已经有一个数据库文件,您可以将它放在 Assets 文件夹中,然后可以使用Assets.Open().

于 2013-12-10T22:18:29.377 回答