jjzjj

mysql - 数据库没有返回正确的值?

coder 2023-10-13 原文

好吧,我目前有一个具有以下时间的数据库:

id 1 startTime 2013-09-09 15:05:10.0 endTime 2013-09-09 15:05:10.0

id 2 startTime 2013-09-09 15:09:54.0 endTime 2013-09-09 15:09:54.0

id 3 startTime 2013-09-09 15:20:46.0 endTime 2013-09-09 15:20:46.0

id 4 startTime 2013-09-09 15:21:06.0 endTime 2013-09-09 15:21:06.0

id 5 startTime 2013-09-09 15:21:34.0 endTime 2013-09-09 15:21:34.0

id 6 startTime 2013-09-09 15:22:34.0 endTime 2013-09-09 15:22:34.0

id 7 startTime 2013-09-09 15:23:06.0 endTime 2013-09-09 15:25:34.0

现在,当我通过位于此处的时间方法运行搜索时:

@Override
public ArrayList<AppointmentAccess> searchByTime(Timestamp startTime,
        Timestamp endTime) throws SQLException {
    ArrayList<AppointmentAccess> appointmentList = new ArrayList<AppointmentAccess>();

    String preparedQuery = "Select DISTINCT * From Appointments where startTime <= appointments.endTime AND endTime >= appointments.startTime";

    // Connect to database
    try (Connection connection = DriverManager.getConnection(url, user,
            password);

    // Run SQL
    PreparedStatement ps = connection.prepareStatement(preparedQuery);

    // Get SQL results
    ResultSet query = ps.executeQuery();) {

        while (query.next()) {
            AppointmentAccess appointment = new AppointmentAccess();
            appointment.setStartTime(query.getTimestamp("starttime"));
            appointment.setEndTime(query.getTimestamp("endtime"));

                appointment.setAlarmReminder(query
                        .getBoolean("alarmreminder"));
                appointment.setAllDay(query.getBoolean("allday"));
                appointment.setDetails(query.getString("details"));
                appointment.setLocation(query.getString("location"));
                appointment.setTitle(query.getString("title"));
                appointmentList.add(appointment);
        }
    }
    // Returns a List of all the contacts
    return appointmentList;
}

我的测试方法“searchTooLate、searchTooEarly 和 searchTimeBetweenAppointments”一直失败。 我发送这些方法的时间是:

开始时间:“2013-09-09 16:05:09” 结束时间:“2013-09-09 16:22:35”

开始时间:“2013-09-09 15:24:06.0” 结束时间:“2013-09-09 15:25:30.0”

开始时间:“2013-08-09 14:05:09” 结束时间:“2013-08-09 16:22:35”

我做错了什么!?

最佳答案

考虑这部分代码:String preparedQuery = "Select DISTINCT * From Appointments where startTime <= appointments.endTime AND endTime >= appointments.startTime";

您认为这将如何改变?您每次都将 startTime 与“Appointments.editTime”进行比较(故意使用双引号)。这会导致查询失败,因为 Appointments.editTime 不是数据库中的变量。

也许你打算 ..." + Appointments.startTime.ToString() + "...这仍然是错误的方法,但这将使您的代码正常运行。考虑一个 PROC 和参数。

关于mysql - 数据库没有返回正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19231916/

有关mysql - 数据库没有返回正确的值?的更多相关文章

  1. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  6. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  7. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  8. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  9. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  10. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

随机推荐