C
C#12mo ago
kevin

Trying to implement SQL database in my app

Hello I'm trying to implement SQLite in my application, my professor has provided me with sample code but whenever i try to implement it i get the error seen in the screenshot, i suppose the package cant find my database file ? Heres the code im using: Constants.cs
public static class Constants
{
private const string dBFileName = "database.db3";

public const SQLiteOpenFlags flags =
SQLiteOpenFlags.ReadWrite |
SQLiteOpenFlags.Create |
SQLiteOpenFlags.SharedCache;

public static string DatabasePath
{
get
{
Console.WriteLine(FileSystem.AppDataDirectory, dBFileName);
return Path.Combine(FileSystem.AppDataDirectory, dBFileName);
}
}
}
public static class Constants
{
private const string dBFileName = "database.db3";

public const SQLiteOpenFlags flags =
SQLiteOpenFlags.ReadWrite |
SQLiteOpenFlags.Create |
SQLiteOpenFlags.SharedCache;

public static string DatabasePath
{
get
{
Console.WriteLine(FileSystem.AppDataDirectory, dBFileName);
return Path.Combine(FileSystem.AppDataDirectory, dBFileName);
}
}
}
3 Replies
kevin
kevinOP12mo ago
Gamerepository.cs
public class GameRepository
{
SQLiteConnection connection;
public GameRepository()
{
connection = new SQLiteConnection(

Constants.DatabasePath,
Constants.flags);
connection.CreateTable<Game>();
}

public void Add(Game game)
{
int result = 0;
try
{
result = connection.Insert(game);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public List<Game>? Read()
{
try
{
return connection.Table<Game>().ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
}
public class GameRepository
{
SQLiteConnection connection;
public GameRepository()
{
connection = new SQLiteConnection(

Constants.DatabasePath,
Constants.flags);
connection.CreateTable<Game>();
}

public void Add(Game game)
{
int result = 0;
try
{
result = connection.Insert(game);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public List<Game>? Read()
{
try
{
return connection.Table<Game>().ToList();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
}
kevin
kevinOP12mo ago
No description
TehBluebear
TehBluebear12mo ago
I can't quite remember off the top of my head what error I was getting, but SQLite didn't really work until I had either an x86 or an x64 folder within my root directory with the correct SQLite.Interop.dll file within it

Did you find this page helpful?