Reputation: 25
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()
Upvotes: 1
Views: 1335
Reputation: 41
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);
Upvotes: 0
Reputation: 5549
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()
.
Upvotes: 0