Note that there are some explanatory texts on larger screens.

plurals
  1. POActiverecord, select on string compare
    primarykey
    data
    text
    <p>I have a table called Basic , start_time is one field with type :VARCHAR(5), which actually stores a 5 bytes time data: byte 0 and 1 map to the year , month and day, and byte 2 to 4 map to the hour, min and second. So, it could possible bytes 2 ,3 ,4 are all 0. And I want to do following query :</p> <pre><code>Basic.find (:all , :conditions =&gt; "start_time &gt; ? AND end_time &lt; ?" , start_time , end_time) </code></pre> <p>Here are the questions:</p> <p>Suppose in VARCHAR(5) format ,the start time is [214, 222,0 ,0, 0] (Jun, 24th, 2009) and the end time is [214, 223, 0, 0, 0] (Jun , 25, 2009).</p> <p>As activerecord maps VARCHAR(5) to String , so in above query the start_time and end_time should also be String. What is the correct way to convert above VARCHAR(5) format time to the String?</p> <p>I did it this way, but fails to get correct result:</p> <pre><code>tmp = [214, 222,0 ,0 ,0].map {|t| t.to_s(16)} ; start_time = tmp.to_s </code></pre> <p>And i was using sqlite3 adapter for activerecord. </p> <p>Thanks for your help.</p> <hr> <p>I have found where the problem is: "\000" is not allowed to be contained in the start_time when do follwing query:</p> <pre><code>Basic.find (:all , :conditions =&gt; "start_time &gt; ? AND end_time &lt; ?" , start_time , end_time) </code></pre> <p>So, I need to do two steps:</p> <ol> <li><p>[214, 222,0 ,0, 0] - > [214,222]</p></li> <li><p>[214,222] -> "\326\336"</p></li> </ol> <p>The 1st steps can be done using:</p> <pre><code>a = [214,222,0,0,0] while a.last ==0 do a.pop end </code></pre> <p>The 2nd steps can be done using:</p> <pre><code>a = [214,222] a.pack("c" * a.size) </code></pre> <p>However, I still can not do query when start_time = [214, 222,0 ,23, 0] , because the corresponding string contains "\000" in the middle. fortunately, It would not be a big problem in our appication , as we never query in hours level, that means last two number will always be 0.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload