1
votes

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

2
please post the details of the exception that you are gettingMauricio Gracia Gutierrez
Narayana...the first line of your screenshot contains more text, You can copy and paste that text into your questionMauricio Gracia Gutierrez
imgur.com/dP9L8Ie Is that the extra you wanted?Narayana James Emery
Narayana it seems that the "whole error message" is just "Could not open database file" ? is the path correct ? does it have permission to be accesed by your program ?Mauricio Gracia Gutierrez

2 Answers

0
votes

I don't think you have the right permission to get a file from a path like this.

To get a file you should do:

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

If you already have a database file, you may put it in Assets folder, then it can be read by using Assets.Open().

0
votes

Note: I'm assuming the code in question is 1) running in a console app, not on a mobile OS. 2) using SQLiteNET (an ORM) rather than using SQLite directly.

The problem appears to be that you're creating an empty file and then expecting sqlite to open it as a database file. If a database file doesn't already exist, you need to let sqlite create it. Here's an example:

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