【问题标题】:SQLite - using Count() to get row count - then use conditionalSQLite - 使用 Count() 获取行数 - 然后使用条件
【发布时间】: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


【解决方案1】:

我发现了错误。我尝试将此方法用作递归方法,但是 for 循环

            else if (rowCount > 1) {
                for (int i = rowCount; i >= 1; i--) {
                    connection.Delete(new User(){ID=i});
                }
                settingTags ();
            }

没有删除行。所以,你好无限循环。我改变了方法如下:

    protected void settingTags() {

        using (var connection = new SQLiteConnection (dbPath)) {

            var rowCount = connection.Table<User> ().Count ();

            var presentUser = connection.Get<User> (1);

            Log.Info (Tag, "rowCount: " + rowCount.ToString ());

            if (rowCount > 1) {
                Log.Info (Tag, "Database cleared");
                for (int i = rowCount; i > 0-1; i--) {
                    //var del = connection.Delete<User> (i);
                    var deleted = connection.Query<User> ("DELETE FROM User WHERE ID = ?", i);
                    Log.Info (Tag, "for loop i: " + i.ToString ());
                    Log.Info (Tag, "Rows deleted: " + deleted.ToString ());
                }
            }

            rowCount = connection.Table<User> ().Count ();

            if (rowCount <= 1) {
                presentUser.Persons = persons;
                connection.Update (presentUser);
                //var Users = connection.Query<User>("UPDATE User SET Persons = ? WHERE ID = 1", persons);
                Log.Info (Tag, "User Data Updated");
            }

        }
    }

现在可以了。 感谢所有试图帮助我的人!


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多