Windows XAML 中的 SQLite (PCL) 查询不允许超过 21 列?

SQLite (PCL) query in Windows XAML not allowing more than 21 columns?

我在我的 Universal Windows 8.1 RunTime 应用程序中使用了 SQLite-PCL
我的项目中有一个很大的 table 和 23 列 。问题是 - 虽然 table-创建和数据插入代码 运行 没有任何错误,但在全列查询中 (SELECT * FROM table) 没有给我超过 21 列。

这是我的创建-table方法(运行没问题):

    private void CreateTable()
    {
        string CREATE_TABLE_SQL = @"CREATE TABLE IF NOT EXISTS "
                + TABLE_NAME
                + "( "
                + KEY_ID + " INTEGER PRIMARY KEY, " // 0
                + KEY_UPDATED_AT + " TEXT, "        // 1
                + KEY_CREATED_AT + " TEXT, "        // 2

            // ... all other column names are stated here in the similar way

                + KEY_LAST_EDITED_BY + " INTEGER, " // 20
                + KEY_LAST_EDIT_TIME + " TEXT, "    // 21
                + KEY_IS_LD + " INTEGER "        // 22
                + ");";
        using (var connection = new SQLiteConnection(DB_NAME))
        {
            using (var statement = connection.Prepare(CREATE_TABLE_SQL))
            {
                statement.Step();
            }
        }
    }

这是我的行插入 SQL 查询(它 运行 也可以):

string insQuery = 
       @"INSERT INTO " + TABLE_NAME
          + " ( " + KEY_ID + ", " //1
          + KEY_UPDATED_AT + ", "//2
          + KEY_CREATED_AT + " , "//3
          // all other col. names are stated here
          + KEY_LAST_EDITED_BY + ", "//21
          + KEY_LAST_EDIT_TIME + ", "//22
          + KEY_IS_LD + " "//23
          + " ) " 
          + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

但是如果我查询所有行或包含所有列的特定行,准备好的语句不能超过索引 20,因此我假设它不包含超过 21 列。 这是在访问第 21 项期间不 运行 OK & 给出 NullReferenceException 的代码:

public List<MyModel> GetAll()
{
    List<MyModel> objList = new List<MyModel>();

    string query = @"SELECT * FROM " + TABLE_NAME
                        + " ORDER BY " + KEY_ID + " DESC;";
    using (var connection = new SQLiteConnection(DB_NAME))
    {
        using (var statement = connection.Prepare(query))
        {
            // The next line prints this: "Statement data-count=0, ColumnCount=23"
            ALog.d("Statement data-count=" + statement.DataCount + 
                    ", ColumnCount=" + statement.ColumnCount); 
            while (statement.Step() == SQLiteResult.ROW)
            {
                try
                {
                    int id = Int32.Parse(statement[0].ToString());
                    string updatedAt = statement[1].ToString();
                    string createdAt = statement[2].ToString();
                    // ... all other values are extracted here, then I'm building
                    //  my user object & the next line prints my expected values
                    ALog.d("User: " + user.ToString());

                    // ... the remaining columns are got here nicely
                    string imgUrl = statement[19].ToString();
                    int lastEdt = Int32.Parse(statement[20].ToString());

                    // This line is the culprit giving out NullReferenceException >_<
                    string lastEdtTm = statement[21].ToString();
                    bool isLd = Int32.Parse(statement[22].ToString()) > 0;

                    objList.Add(new MyModel(
                        // Params. of the constructor goes here ...
                     ));
                }
                catch (Exception e)
                {
                    ALog.d("Db - GetAll() : Exception:: " + e.ToString());
                }
            }

            // This line prints as: "Statement data-count=23, ColumnCount=23"
            // That means - all my data & columns have been queried,
            // but I couldn't read values from statement[21] & statement[22]
                ALog.d("Statement data-count=" + statement.DataCount + 
                        ", ColumnCount=" + statement.ColumnCount);
                statement.Reset();
                statement.ClearBindings();
        }
    }

    return objList;
}

请注意,我在项目中有超过 10 个 table 少于 17 列并且所有这些都工作正常。

问题与这样的行有关:

string lastEdtTm = statement[21].ToString();

这里,如果statement[21] return是一个null值,那么相当于null.ToString(),会抛出异常。

轻松修复:

string lastEdtTm = statement[21] == null ? "" : statement[21].ToString();

如果 statement[21] 解析为 null,这将 return 为空字符串,否则为字符串值。

请注意,您应该对 所有可能 return 为空的列执行此操作 - 只是因为您现在在其他地方没有得到该异常,不会意味着您稍后可能无法在添加新行时获得它,这可能会丢失其他值。