首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >指定的C# SQLite和SQL命令转换无效

指定的C# SQLite和SQL命令转换无效
EN

Stack Overflow用户
提问于 2011-05-23 16:37:48
回答 3查看 5.3K关注 0票数 0

我正在尝试使用SQlite和sql命令。我正在尝试做一个测验程序,我有一个循环,它从我的数据库中读取问题和答案,并将它们添加到列表中。我还有一个bool,它定义从数据库中选择的答案是正确的还是错误的。

我的问题是,当我的循环第一次执行代码并将true和false添加到布尔数组中时,所有这些都工作得很好,但第二次执行时抛出了异常:指定的强制转换无效。失败的方法如下所示:我在代码失败的地方做了一个注释:

代码语言:javascript
运行
复制
    public void GetQuestion(int categoryRef)
    {
        Console.Clear();

        int arrayIndex = 0;

        int qListIndex = 0;

        int idListIndex = 0;

        List<string> qList = new List<string>();

        List<int> idList = new List<int>();

        int ansNr = 1;

        bool[] isTrue = new bool[3];


        SQLiteDataReader sqReader;

        SQLiteCommand sqCommand = new SQLiteCommand(sqConnection);

        try
        {
            sqCommand.CommandText = "SELECT Question, ID FROM Questions WHERE CategoryRef=" + categoryRef.ToString();
            sqCommand.Connection.Open();
            sqReader = sqCommand.ExecuteReader();
            foreach (var item in sqReader)
            {
                qList.Add(sqReader.GetString(0));
                idList.Add(sqReader.GetInt32(1));
            }
            sqReader.Close();
        }
        finally
        {
            sqConnection.Close();    
        }

        for (int i = 0; i < qList.Count; i++)
        {   
            try
            {
                sqCommand.CommandText = "SELECT Answer FROM Answers WHERE QuestionRef=" + idList[idListIndex].ToString();
                sqConnection.Open();
                sqReader = sqCommand.ExecuteReader();
                Console.WriteLine(qList[qListIndex]);
                foreach (var answer in sqReader)
                {
                    Console.WriteLine(ansNr + ":" + sqReader.GetString(0));

                    ansNr++;
                }
                sqReader.Close();
            }
            finally
            {
                sqConnection.Close();
            }

            try
            {   
                //THIS CODE FAILS 2'nd TIME IT LOOPS THROUGH
                sqCommand.CommandText = "SELECT IsTrue FROM Answers WHERE QuestionRef=" + idList[idListIndex].ToString();
                sqConnection.Open();
                sqReader = sqCommand.ExecuteReader();
                foreach (var item in sqReader)
                {

                    isTrue[arrayIndex] = sqReader.GetBoolean(0); //<-- Specified cast is not valid.
                    arrayIndex++;


                }
                sqReader.Close();
            }
            finally
            {

                sqConnection.Close();
            }

            string input = Console.ReadLine();
            int number = Convert.ToInt32(input);

            switch (number)
            {
                case 1:
                    if (isTrue[0] == true)
                    {
                        Console.WriteLine("Correct");
                    }
                    if (isTrue[0] == false)
                    {
                        Console.WriteLine("False");
                    }
                    break;

                case 2:
                    if (isTrue[1] == true)
                    {
                        Console.WriteLine("Correct");
                    }
                    if (isTrue[1] == false)
                    {
                        Console.WriteLine("False");
                    }
                    break;

                case 3:
                    if (isTrue[2] == true)
                    {
                        Console.WriteLine("Correct");
                    }
                    if (isTrue[2] == false)
                    {
                        Console.WriteLine("False");
                    }
                    break;
            }
            Console.ReadLine();
            idListIndex++;
            qListIndex++;
            arrayIndex = 0;
            ansNr = 1;
        }
    }
EN

Stack Overflow用户

发布于 2011-10-01 05:38:00

SQLite不保证任何列的内容都与强制转换匹配。

GetBoolean出现故障。你有3个选择。

  1. Try Catch
  2. 测试空
  3. 将您的select替换为保证返回true/false的查询

SQLite version 3.7.8 2011-09-19 14:49:19

sqlite>创建表Q (A);

sqlite>插入Q值(0);

sqlite>插入Q值(1);

sqlite>插入Q值('T');

sqlite>插入Q值('F');

sqlite>插入Q值('Y');

sqlite>插入Q值(NULL);

sqlite> SELECT not ifnull(A==0 OR A=='F‘OR A=='N',1) FROM Q;

0

1

1

0

1

0

票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6094761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档