【发布时间】:2016-01-03 23:07:17
【问题描述】:
编辑3: 我找到了答案。我感谢大家的帮助!这有点愚蠢,但我猜每个人有时都会犯这些错误。
问题:
我正在使用适用于 Android 的 Xamarin 和适用于 SQL 的 Sqlite-net Nuget,并且具有以下代码
protected void settingTags() {
using (var connection = new SQLiteConnection (dbPath)) {
var rowCount = connection.Table<User> ().Count ();
if (rowCount <= 1) {
// Setting the column "Person" of "Row where Primary Key is
// "1" to the variable "person" in the table "User"
// This gets the Row where the primary key attribute
// is "1"
var presentUser = connection.Get<User> (1);
// This sets the column "Persons" to "persons" where
// person is an integer and is 1
presentUser.Persons = persons;
// This updates the Database
connection.Update (presentUser);
Log.Info (Tag, "User Data Updated");
}
else if (rowCount > 1) {
for (int i = rowCount; i >= 1; i--) {
connection.Delete(new User(){ID=i});
}
settingTags ();
}
}
}
这给了我一个空异常错误。 我使用断点来查明错误,并且不知何故它在行中:
if (rowCount <= 1) {
当然还有其他条件。
编辑2: 但实际的错误在于:
var presentUser = connection.Get<User> (1);
我使用了文档中使用的代码。 并且有一行主键为“1” ...
非常感谢任何帮助。 对不起,如果我在这个问题上使用了错误的标签或类似的东西,因为这是我的第一个问题。 :s
编辑:rowCount 是 6。所以它有一个值。
【问题讨论】:
-
rowCount 小于 1 的值是多少?如果这个条件成立,你会怎么做?
-
根据您使用断点进行的调试,
rowCount的值是多少? -
对不起,我的错。它是 rowCount
-
调试器有时会在出现错误的行之后中断。所以对
Count的调用可能会失败。 -
你能解释一下这条线是什么意思吗? connection.Get
(1); 如果你的 rowCount 在这里是零会发生什么?您能否添加收到的错误消息的确切拼写?
标签: c# android sql sqlite xamarin