Sequel 支持一个boolean
类型。它将true
和存储false
在SQLite 中t
。f
如果您再次阅读,数据将转换回true
和false
SQLite 本身更喜欢存储true
为 1 和false
0(一些 SQLite 管理工具期望它)。如果我将布尔值也存储为“1”,Sequel 也会将该值转换为“真”:
require 'sequel'
DB = Sequel.sqlite()#'test.db')
DB.create_table(:test){
boolean :my_truth
nvarchar :descr, :size => 10
}
DB[:test].insert(1, '1')
p DB[:test].filter(:descr => '1').first #-> {:my_truth=>true, :descr=>"1"}
但是,如果我选择 for true
,则找不到任何值:
DB[:test].filter(:my_truth => true).each{|ds|
puts "\t%s" % ds[:descr]
}
值true
和"t"
被发现:
DB[:test].insert(true, 'true')
DB[:test].insert('t', '"t"')
DB[:test].filter(:my_truth => true).each{|ds|
puts "\t%s" % ds[:descr] #true and 't'
}
假值也有类似的情况0
......(请参阅问题后的示例代码)
所以我的问题是:我怎样才能DB[:test].filter(:my_truth => true)
检测1
-values和
DB[:test].filter(:my_truth => false)
检测0
-values?
我不是在寻找类似的东西DB[:test].filter(:my_truth => [true,1])
Activerecord Rails 3 SQLite3 Boolean false的类似问题
我使用续集 3.33.0
示例代码:
require 'sequel'
DB = Sequel.sqlite()#'test.db')
DB.create_table(:test){
boolean :my_truth
nvarchar :descr, :size => 10
fixnum :line #code line, where data is inserted
}
#All true:
DB[:test].insert(true, 'true', __LINE__)
DB[:test].insert('true', '"true"', __LINE__)
DB[:test].insert(1, 'one', __LINE__)
DB[:test].insert('t', 't', __LINE__)
#All false:
DB[:test].insert(false,'false', __LINE__)
DB[:test].insert('false','"false"', __LINE__)
DB[:test].insert(0,'zero', __LINE__)
DB[:test].insert('f', 'f', __LINE__)
DB[:test].insert('F', 'F', __LINE__)
DB[:test].insert(nil, 'nil', __LINE__)
DB[:test].insert('n', 'n', __LINE__)
DB[:test].insert('N', 'N', __LINE__)
#Also true
DB[:test].insert('x', 'x', __LINE__)
DB[:test].insert(' ', 'space', __LINE__)
DB[:test].insert('', 'empty', __LINE__)
puts "All true values:"
DB[:test].filter(:my_truth => true).each{|ds|
puts "\t%s (line %i)" % [ds[:descr], ds[:line] ]
}
puts "All false values:"
DB[:test].filter(:my_truth => false).each{|ds|
puts "\t%s (line %i)" % [ds[:descr], ds[:line] ]
}
puts "Data:"
DB[:test].each{|ds|
puts "\t%-5s is <%s> (line %i)" % [ ds[:descr], ds[:my_truth].inspect, ds[:line] ]
}
结果:
All true values:
true (line 10)
t (line 13)
All false values:
false (line 16)
f (line 19)
Data:
true is <true> (line 10)
"true" is <true> (line 11)
one is <true> (line 12)
t is <true> (line 13)
false is <false> (line 16)
"false" is <false> (line 17)
zero is <false> (line 18)
f is <false> (line 19)
F is <false> (line 20)
nil is <nil> (line 21)
n is <false> (line 22)
N is <false> (line 23)
x is <true> (line 26)
space is <true> (line 27)
empty is <true> (line 28)