1

sqlite データベース用のプリローダー C# プログラムを作成しようとしています。ファイルを作成してから読み取ると、例外が発生します。削除して新しいものを作成すると、例外が発生します。誰かが私が間違っていることを教えてもらえますか?!

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

最後の行は、例外がスローされる場所です。

スタックトレース:

c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\Preloader\SqliteNet.cs:line 153 の SQLite.SQLiteConnection の SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks) で。 .ctor(String databasePath, Boolean storeDateTimeAsTicks) in c:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\Preloader\SqliteNet.cs:114 行目 Preloader.Program.Main(String[] args) のc:\Users\Sgt.Waffles\Documents\Visual Studio 2013\Projects\TidesDatabase\Preloader\Program.cs:System.AppDomain._nExecuteAssembly(RuntimeAssembly アセンブリ、String[] args) の System.AppDomain.ExecuteAssembly(String) の 40 行目assemblyFile、Evidence assemblySecurity、String[] args) (System.Threading.ThreadHelper の Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly())。System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態、Boolean preserveSyncCtx) での ThreadStart_Context(オブジェクト状態) System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback コールバック、オブジェクト状態、Boolean preserveSyncCtx) で System. System.Threading.ThreadHelper.ThreadStart() での Threading.ExecutionContext.Run (ExecutionContext 実行コンテキスト、ContextCallback コールバック、オブジェクト状態)System.Threading.ThreadHelper.ThreadStart() での ExecutionContext.Run (ExecutionContext 実行コンテキスト、ContextCallback コールバック、オブジェクト状態)System.Threading.ThreadHelper.ThreadStart() での ExecutionContext.Run (ExecutionContext 実行コンテキスト、ContextCallback コールバック、オブジェクト状態)

4

2 に答える 2

0

このようなパスからファイルを取得するための適切な権限がないと思います。

ファイルを取得するには、次のことを行う必要があります。

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

既にデータベース ファイルがある場合は、それを Assets フォルダーに配置すると、 を使用して読み取ることができますAssets.Open()

于 2013-12-10T22:18:29.377 に答える
0

注: 問題のコードは、1) モバイル OS ではなく、コンソール アプリで実行されていると想定しています。2) SQLite を直接使用するのではなく、SQLiteNET (ORM) を使用する。

問題は、空のファイルを作成していて、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 に答える