首页 > 解决方案 > SQLite 数据库游标始终返回 0,即使表已正确填充

问题描述

我已将数据保存在表 PollCollection(PollID, PollName, VotingClass) 和 OptionCollection(OptionID, PollID, Option) 中。我有一个名为 Poll 的类,其中可以保存字符串 PollTitle 和 LinkedList 选项(以及其他选项)。我使用适当的 VotingClass 从 PollCollection 中读取了我的第一个 PollName,然后尝试从其中 PollID = PollCollection.PollID 的 OptionCollection 中读取 Option。但是,我的选项光标的长度为 0。

我知道 OptionCollection 已正确保存的事实,因为在另一段代码中,我设法运行了 select * 查询并打印了所有适当的值。在这一点上,我不知道我错过了什么。另外,我正在使用 Xamarin Android。

string pollquery = "";
        string optionsquery = "";
        pollquery = string.Format("select * from PollCollection where PollCollection.VotingClass = '{0}'", userclass);
        ICursor polls = MainActivity.sqlitedb.RawQuery(pollquery, null);
        if (polls.Count > 0)
        {
            polls.MoveToFirst();
            do
            {
                Poll currentpoll = new Poll();
                currentpoll.PollTitle = polls.GetString(polls.GetColumnIndex("PollName"));
                string pollid = polls.GetString(polls.GetColumnIndex("PollID"));
                optionsquery = string.Format("select Option from OptionCollection where OptionCollection.PollID = '{0}'", pollid);
                ICursor options = MainActivity.sqlitedb.RawQuery(optionsquery, new string[] { });
                options.MoveToFirst();
                do
                {
                    currentpoll.Options.AddLast(options.GetString(options.GetColumnIndex("Option")));
                } while(options.MoveToNext());
                polllist.AddLast(currentpoll);
            } while (polls.MoveToNext());
        }  

标签: c#androiddatabasesqlitexamarin

解决方案


推荐阅读