Ibr*_*hab 4 c# database sqlite xamarin xamarin.forms
对不起,如果问题之前被问到,但我搜索得很好,以解决这个问题,我没有找到答案.
我在我的Xamarin表单项目(PCL)中使用SQLite处理本地数据库.
1-连接在iOS中运行良好但在android中我遇到了这个问题(无法打开数据库文件)
2-我使用了另一种创建连接路径的方法:
string documentsPath = System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal);
string path = Path.Combine(documentsPath, "pbcare.db");
Run Code Online (Sandbox Code Playgroud)
通过这种方式,处理数据库时发生了异常......
public bool checkLogin (string email, string password)
{
if (DB.Table<User> ().Where (user => user.Email == email && user.Password == password)
.FirstOrDefault () != null) { // exception caught here
return true;
} else {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
但是数据库中有表格.
注意:即使数据库文件不存在,第二种方式也会创建连接!
小智 10
Android将无法将SQLite数据库与本地系统中的文件一起使用.该path变量必须来自Android系统.您用于创建路径的第二种方法是正确的:
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
path = Path.combine(path, 'pbcare.db3');
Run Code Online (Sandbox Code Playgroud)
将在Android上为db3文件创建适当的文件路径.
您引用的下一个问题:no such table: User是由于没有创建表而引起的.在使用数据库之前,需要创建所有必需的表.
var conn = new SQLiteConnection(path);
conn.CreateTable<User>();
Run Code Online (Sandbox Code Playgroud)
如果您这样做并User首先创建表,那么它应该按预期工作.Xamarin有一个更深入的教程:https://developer.xamarin.com/guides/cross-platform/application_fundamentals/data/part_3_using_sqlite_orm/