当 table 存在于 xamarin 中时,SQLite 没有这样的 table 错误
SQLite no such table error when table exists in xamarin
这就是我如何定义我的 table 作为这个 link: SQLite no such table error when table exists 所说
[Table("RegUserTable")]
[Serializable]
[DataContract]
public class RegUserTable
{
[PrimaryKey]
[DataMember]
public Guid UserId { get; set; }
[DataMember]
public string Username { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Gender { get; set; }
}
这是我的登录码:
public void Button_Clicked_1(object sender, EventArgs e)//LOGIN!
{
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Userdatabase.db");
var db = new SQLiteConnection(dbpath);
var loginquery = db.Table<RegUserTable>().Where(u => u.Username.Equals(EntryLoginUsername.Text) && u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();
if (string.IsNullOrWhiteSpace(EntryLoginUsername.Text) && string.IsNullOrWhiteSpace(EntryLoginPassword.Text))
{
DisplayAlert("Blank Fields", "Please Input Your Username and Password!", "OK");
}
else if (loginquery != null)
{
App.Current.MainPage = new NavigationPage(new MainPage(EntryLoginUsername.Text, GenderIdentifier.Text));
}
}
如果我在我的模拟器中 运行 它可以 100% 工作,但是当我在我的设备上 运行 它会抛出这个错误:
我在这里做错了什么?
创建如下所示的 table 可以解决问题
public async void Button_Clicked_1(object sender, EventArgs e)//LOGIN!
{
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Userdatabase.db");
var db = new SQLiteConnection(dbpath);
var connection = new SQLiteAsyncConnection(dbpath);
await connection.CreateTableAsync<RegUserTable>();
var loginquery = db.Table<RegUserTable>().Where(u => u.Username.Equals(EntryLoginUsername.Text) && u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();
if (string.IsNullOrWhiteSpace(EntryLoginUsername.Text) && string.IsNullOrWhiteSpace(EntryLoginPassword.Text))
{
DisplayAlert("Blank Fields", "Please Input Your Username and Password!", "OK");
}
else if (loginquery != null)
{
App.Current.MainPage = new NavigationPage(new MainPage(EntryLoginUsername.Text, GenderIdentifier.Text));
}
}
这就是我如何定义我的 table 作为这个 link: SQLite no such table error when table exists 所说
[Table("RegUserTable")]
[Serializable]
[DataContract]
public class RegUserTable
{
[PrimaryKey]
[DataMember]
public Guid UserId { get; set; }
[DataMember]
public string Username { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public string Gender { get; set; }
}
这是我的登录码:
public void Button_Clicked_1(object sender, EventArgs e)//LOGIN!
{
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Userdatabase.db");
var db = new SQLiteConnection(dbpath);
var loginquery = db.Table<RegUserTable>().Where(u => u.Username.Equals(EntryLoginUsername.Text) && u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();
if (string.IsNullOrWhiteSpace(EntryLoginUsername.Text) && string.IsNullOrWhiteSpace(EntryLoginPassword.Text))
{
DisplayAlert("Blank Fields", "Please Input Your Username and Password!", "OK");
}
else if (loginquery != null)
{
App.Current.MainPage = new NavigationPage(new MainPage(EntryLoginUsername.Text, GenderIdentifier.Text));
}
}
如果我在我的模拟器中 运行 它可以 100% 工作,但是当我在我的设备上 运行 它会抛出这个错误:
我在这里做错了什么?
创建如下所示的 table 可以解决问题
public async void Button_Clicked_1(object sender, EventArgs e)//LOGIN!
{
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Userdatabase.db");
var db = new SQLiteConnection(dbpath);
var connection = new SQLiteAsyncConnection(dbpath);
await connection.CreateTableAsync<RegUserTable>();
var loginquery = db.Table<RegUserTable>().Where(u => u.Username.Equals(EntryLoginUsername.Text) && u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();
if (string.IsNullOrWhiteSpace(EntryLoginUsername.Text) && string.IsNullOrWhiteSpace(EntryLoginPassword.Text))
{
DisplayAlert("Blank Fields", "Please Input Your Username and Password!", "OK");
}
else if (loginquery != null)
{
App.Current.MainPage = new NavigationPage(new MainPage(EntryLoginUsername.Text, GenderIdentifier.Text));
}
}