[英]SQLite.Net SQLiteException Constraint
I've looked here before with this error but the results won't work for me.我以前在这里看过这个错误,但结果对我不起作用。
I have made a login form and it works fine for one time.我制作了一个登录表单,并且一次运行正常。 After I logged in once and restart the app and try to log in I'm getting the error.
在我登录一次并重新启动应用程序并尝试登录后,我收到错误消息。
Function:功能:
public int SaveUser(User user)
{
lock (locker)
{
if (user.Id != 0)
{
database.Update(user);
return user.Id;
}
else
{
return database.Insert(user);
}
}
}
User model:用户模型:
[Table("User")]
public class User
{
[PrimaryKey, AutoIncrement, NotNull]
public int Id { get; set; }
[Unique]
public string Username { get; set; }
public string Password { get; set; }
}
Update: I hope this are the details that's needed for further help.更新:我希望这是进一步帮助所需的详细信息。
02-27 14:13:15.806 I/MonoDroid(23447): at SQLite.PreparedSqlLiteInsertCommand.ExecuteNonQuery (System.Object[] source) [0x00121] in /Users/vagrant/git/src/SQLite.cs:2668 02-27 14:13:15.806 I/MonoDroid(23447): at SQLite.SQLiteConnection.Insert (System.Object obj, System.String extra, System.Type objType) [0x000f4] in /Users/vagrant/git/src/SQLite.cs:1415 02-27 14:13:15.806 I/MonoDroid(23447): at SQLite.SQLiteConnection.Insert (System.Object obj) [0x00005] in /Users/vagrant/git/src/SQLite.cs:1281 02-27 14:13:15.806 I/MonoDroid(23447): at KleynGroup.Data.UserDatabaseController.SaveUser (KleynGroup.User user) [0x00038] in C:\\Users\\[name]\\source\\repos\\KleynGroup\\KleynGroup\\KleynGroup\\Data\\UserDatabaseController.cs:48
Sorry if there are a lot of mistakes.如果有很多错误,请见谅。 I'm new to Xamarin and still learning.
我是 Xamarin 的新手,仍在学习。
I hope you guys can help me我希望你们能帮助我
According to the error messages the exception is thrown by Insert
, hence I'd assume that you are trying to insert the User
( User.Id
seems to be 0
).根据错误消息,异常是由
Insert
抛出的,因此我假设您正在尝试插入User
( User.Id
似乎是0
)。 Anyway, since it's not a new, but an existing user name, inserting it fails because of the UNIQUE
constraint of Username
.无论如何,由于它不是一个新的用户名,而是一个现有的用户名,由于
Username
的UNIQUE
约束,插入它会失败。
You have to make sure, that when the user is loaded and then saved, it still has the correct ID, to prevent that exception.您必须确保当用户被加载然后保存时,它仍然具有正确的 ID,以防止出现异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.