Xamarin中的SQLite抛出连接异常
问题描述:
我遇到问题。我在iOS和Android应用程序中使用Xamarin。但是当我尝试建立到文件夹路径的连接时,它总是会引发异常。这是我的代码: public class Queries { private string folder = DependencyService.Get()。GetAppDataFolder();Xamarin中的SQLite抛出连接异常
public bool createDataBase()
{
try
{
using (var connection = new SQLiteConnection(Path.Combine(folder, "Test.db")))
{
connection.CreateTable<Posten>();
return true;
}
}
catch (SQLiteException ex)
{
System.Diagnostics.Debug.WriteLine("SQLiteEx", ex.Message);
return false;
}
}
}
它在使用中引发异常。我用的是DependencyService得到我想要把我的数据库路径
private string folder = DependencyService.Get<IFileSystemService>().GetAppDataFolder();
在Android上侧(我目前只是测试机器人)我已经实现了它这样的:
public class FileSystemServiceAndroid : IFileSystemService
{
public string GetAppDataFolder()
{
return System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
}
}
这是异常说:
未处理的异常:
System.TypeInitializationException:本典型初始值>'SQLite.SQLiteConnection'引发异常。发生
答
在我的项目有SQLite.PCL Xamarin.Android,我使用以下来获取数据库文件夹和文件位置:
String dbFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/project_name/";
String dbFileName = "project_name.db";
String dbPath = dbFolder + dbFileName;
//To check if the database file exists: System.IO.File.Exists(dbPath);
//To delete the database file: System.IO.File.Delete(dbPath);
SQLiteConnection dbConn = new SQLiteConnection(dbPath);
什么说异常? –
何时发生异常?在构建或运行时? –
仅在运行时将其部署到我的设备上 –