【问题标题】:SQLite in Xamarin throws exception in connectionXamarin 中的 SQLite 在连接中引发异常
【发布时间】:2017-09-02 15:19:11
【问题描述】:

我有问题。我正在为 iOS 和 Android 应用程序使用 Xamarin。但是当我尝试建立与文件夹路径的连接时,它总是会引发异常。这是我的代码: 公共类查询 { 私有字符串文件夹 = DependencyService.Get().GetAppDataFolder();

    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 端(我目前正在测试 android)我已经这样实现它:

public class FileSystemServiceAndroid : IFileSystemService
{
    public string GetAppDataFolder()
    {
        return System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    }
}

这就是异常所说的:

未处理的异常:

System.TypeInitializationException:>'SQLite.SQLiteConnection' 的类型初始化程序引发了异常。发生了

我刚刚将我尝试访问 createDataBase 方法的代码移到了我的工具栏的初始化位置,并且我在应用程序中得到了一个消息框:

【问题讨论】:

  • 异常怎么说?
  • 什么时候出现异常?在构建或运行时?
  • 就在我在设备上部署它的运行时
  • 您能否从调试输出中提供更多有关异常的详细信息?
  • 你使用哪个 sqlite 包?

标签: c# sqlite xamarin xamarin.ios xamarin.android


【解决方案1】:

在我在 Xamarin.Android 中使用 SQLite.PCL 的项目中,我使用以下内容获取数据库文件夹和文件位置:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多