【问题标题】:SQLite - Could not open database fileSQLite - 无法打开数据库文件
【发布时间】:2014-11-11 00:47:57
【问题描述】:

我正在编写 Xamarin Android 应用程序,但在尝试创建 SQLite 数据库时出现错误。

这是我的代码:

string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "CanFindLocation");
string databaseFileName = System.IO.Path.Combine(applicationFolderPath, "CanFindLocation.db");
SQLite.SQLite3.Config(SQLite.SQLite3.ConfigOption.Serialized);
var db = new SQLiteConnection (databaseFileName);

这是我得到的错误:

SQLite.SQLiteException: Could not open database file: /data/data/com.xamarin.docs.android.mapsandlocationdemo2/files/CanFindLocation/CanFindLocation.db (CannotOpen)

我在另一个 Xamarin 应用程序中运行相同的代码,想知道异常是否与包名称有关?

提前致谢

【问题讨论】:

  • 点击你的数据库可见右上角菜单 Sql lite Browser 选择它

标签: android exception sqlite xamarin android-sqlite


【解决方案1】:

您提供给 SQLite 的路径文件夹路径是否存在?如果您尚未创建 CanFindLocation 文件夹,则打开与该路径的连接将失败。

试试:

string applicationFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "CanFindLocation");

// Create the folder path.
System.IO.Directory.CreateDirectory(applicationFolderPath);

string databaseFileName = System.IO.Path.Combine(applicationFolderPath, "CanFindLocation.db");
SQLite.SQLite3.Config(SQLite.SQLite3.ConfigOption.Serialized);
var db = new SQLiteConnection (databaseFileName);

【讨论】:

  • 谢谢,希望我能多次投票,花了几个小时才找到这篇文章!
  • 我得到同样的错误,...但是 SQLite.SQLite3 没有“配置”方法,为什么?我正在使用“sqlite-net-pcl (1.4.118)”和“Xamarin.Forms(3.1.0)”
  • 我发现有时在调试时,路径神奇地不存在,然后我再次运行它就可以了。打开和关闭
  • 搞笑的是,我刚刚通过 Google 重新发现了这个问题,并用我自己的答案解决了我自己的问题,但一开始并没有意识到……Doh!
【解决方案2】:

我的问题与在初始化期间添加了“SQLiteOpenFlags.Create”选项有关。

SQLiteAsyncConnection database = new SQLiteAsyncConnection(dbPath, SQLiteOpenFlags.Create);

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 2021-10-15
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多