Boolean values โ€‹โ€‹with SQLite and continuation

Continuation supports the boolean type. It stores true and false as t and f in SQLite. If you read again, the data will be converted back to true and false

SQLite itself prefers to store true as 1 and false as 0 (and some SQLite-administartion tools expect it). If I store the boolean value also as "1", Sequel also converts the value to "true":

 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"} 

But if I select for true , no value will be found:

 DB[:test].filter(:my_truth => true).each{|ds| puts "\t%s" % ds[:descr] } 

Found true and "t" values:

 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' } 

A similar situation for false values โ€‹โ€‹such as 0 ... (see sample code after question)

So my question is : How can I do DB[:test].filter(:my_truth => true) to detect 1 values โ€‹โ€‹and DB[:test].filter(:my_truth => false) to detect 0 values?

I'm not looking for something like DB[:test].filter(:my_truth => [true,1])

A similar question for Activerecord Rails 3 SQLite3 Boolean false

I am using Sequel 3.33.0


Code example:

 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] ] } 

Result:

 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) 
+4
source share
1 answer

You can use the integer_booleans parameter to use 1/0 as true / false, instead of 't' / 'f', see http://sequel.rubyforge.org/rdoc-adapters/classes/Sequel/SQLite/DatabaseMethods.html . Here is an example:

 DB = Sequel.sqlite(:integer_booleans=>true) 
+4
source

Source: https://habr.com/ru/post/1414111/


All Articles