From: Michel Date: 2012-04-28T22:20:59+09:00 Subject: Re: ruby/shoes GUI - sqlite, how to isert a variable key in table? --=-h9WA8wCfgmJIYFJKFvOU Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit > >From what I've read[1], sqlite will coerce values into the declared > datatype of the column[2]. > > 1: http://viewsourcecode.org/why/hacking/aQuickGuideToSQLite.html > 2: http://www.sqlite.org/different.html#typing > It seems that sqlite doesn't have the same behavior in Linux (fedora) and windows 7. With this script : require 'sqlite3' var1 = '1' var2 = 2 var3 = '3' var4 = 4 puts "var2 (#{var2.class}) = #{var2} --- var4 (#{var4.class}) = #{var4}" db = SQLite3::Database.new "Ttest.db" db.execute "create table t1 (text TEXT, num INTEGER)" db.execute "insert into t1 (text, num) values (#{var1}, #{var2})" db.execute "insert into t1 values ( ?, ?)", var3, var4 db.execute "insert into t1 (text, num) values (5, 6)" rows = db.execute "select text, num from t1" rows.each{|t,n| puts "#{t.class} : #{t}, #{n.class} : #{n}"} db.close With linux, ruby 1.8.6, I got : var2 (Fixnum) = 2 --- var4 (Fixnum) = 4 String : 1, String : 2 String : 3, String : 4 String : 5, String : 6 But with Windows 7, ruby 1.8.7, I got : var2 (Fixnum) = 2 --- var4 (Fixnum) = 4 String : 1, Fixnum : 2 String : 3, Fixnum : 4 String : 5, Fixnum : 6 Somebody knows why ? Regards, Michel --=-h9WA8wCfgmJIYFJKFvOU Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 7bit
>From what I've read[1], sqlite will coerce values into the declared
datatype of the column[2].

1: http://viewsourcecode.org/why/hacking/aQuickGuideToSQLite.html
2: http://www.sqlite.org/different.html#typing

It seems that sqlite doesn't have the same behavior in Linux (fedora) and windows 7.
With this script :

require 'sqlite3'
  var1 = '1'
  var2 = 2
  var3 = '3'
  var4 = 4
 
  puts "var2 (#{var2.class}) = #{var2} --- var4 (#{var4.class}) = #{var4}"
  db = SQLite3::Database.new "Ttest.db"
  db.execute "create table t1 (text TEXT, num INTEGER)"
  db.execute "insert into t1 (text, num) values (#{var1}, #{var2})"
  db.execute "insert into t1 values ( ?, ?)", var3, var4
  db.execute "insert into t1 (text, num) values (5, 6)"
  rows = db.execute "select text, num from t1"
  rows.each{|t,n| puts "#{t.class} : #{t}, #{n.class} : #{n}"}
  db.close

With linux, ruby 1.8.6, I got :
var2 (Fixnum) = 2 --- var4 (Fixnum) = 4
String : 1, String : 2
String : 3, String : 4
String : 5, String : 6

But with Windows 7, ruby 1.8.7, I got :
var2 (Fixnum) = 2 --- var4 (Fixnum) = 4
String : 1, Fixnum : 2
String : 3, Fixnum : 4
String : 5, Fixnum : 6

Somebody knows why ?

Regards,
Michel



--=-h9WA8wCfgmJIYFJKFvOU--