Content preview: On 29.11.2010 12:39, Brian C. wrote: > Ergh. I
can replicate
this, with ruby 1.9.2p0 (rvm) + sqlite3-ruby-1.3.2 > (gem) under
Ubuntu Lucid
x86_64. > > ruby-1.9.2-p0 > require ‘sqlite3’ > => true >
ruby-1.9.2-p0 >
db = SQLite3::Database.new(“foo.db”) > =>
#SQLite3::Database:0x00000002631bc0
> ruby-1.9.2-p0 > db.execute(“create table foo (id integer
auto_increment,
> bar varchar(255))”) > => [] > ruby-1.9.2-p0 > db.execute(“insert
into foo
(bar) values (‘hello’)”) > => [] > ruby-1.9.2-p0 > s1, s2, s3 =
“hello”,
“hello”.force_encoding(“US-ASCII”),
“hello”.force_encoding(“ASCII-8BIT”)
=> [“hello”, “hello”, “hello”] > ruby-1.9.2-p0 > s1 == s2 > =>
true > ruby-1.9.2-p0
s1 == s3 > => true > ruby-1.9.2-p0 > db.execute(“select * from foo
where
bar=?”, s1) > => [[nil, “hello”]] > ruby-1.9.2-p0 >
db.execute(“select *
from foo where bar=?”, s2) > => [[nil, “hello”]] > ruby-1.9.2-p0 >
db.execute(“select
* from foo where bar=?”, s3) > => [] > > So s1 == s3, but the two
queries
give different results?? This is > barking mad. Having said that, I
can’t
find anything in the sqlite3-ruby > documentation which says how
queries
may be affected by encodings, so > the behaviour is undefined. […]
Content analysis details: (-2.6 points, 5.0 required)
pts rule name description
-1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP
-1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1%
[score: 0.0000]
0.0 T_FILL_THIS_FORM_SHORT Fill in a short form with personal
information
0.3 FILL_THIS_FORM_FRAUD_PHISH Answer suspicious question(s)
X-Cloudmark-Analysis: v=1.1
cv=kRmApA04uPli/d8B8u1M0swu98hronvs6VvAqI8uQP0= c=1 sm=0
a=svAFAT5c7LwA:10 a=n5_bRNUYKd4A:10 a=IkcTkHD0fZMA:10
a=ifuZSf0M0LJmHL7QMhgA:9 a=5xJxjBpEekpQfdk-FI8A:7
a=OicNPj6H6IkURmuq_doO-2SJYXsA:4 a=QEXdDO2ut3YA:10
a=HpAAvcLHHh0Zw7uRqdWCyQ==:117
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Precedence: bulk
Lines: 110
List-Id: ruby-talk.ruby-lang.org
List-Software: fml [fml 4.0.3 release (20011202/4.0.3)]
List-Post: mailto:[email protected]
List-Owner: mailto:[email protected]
List-Help: mailto:[email protected]?body=help
List-Unsubscribe: mailto:[email protected]?body=unsubscribe
Received-SPF: none (Address does not pass the Sender Policy Framework)
SPF=FROM;
[email protected];
remoteip=::ffff:221.186.184.68;
remotehost=carbon.ruby-lang.org;
helo=carbon.ruby-lang.org;
receiver=eq4.andreas-s.net;
On 29.11.2010 12:39, Brian C. wrote:
ruby-1.9.2-p0 > db.execute(“insert into foo (bar) values (‘hello’)”)
ruby-1.9.2-p0 > db.execute(“select * from foo where bar=?”, s2)
=> [[nil, “hello”]]
ruby-1.9.2-p0 > db.execute(“select * from foo where bar=?”, s3)
=> []
So s1 == s3, but the two queries give different results?? This is
barking mad. Having said that, I can’t find anything in the sqlite3-ruby
documentation which says how queries may be affected by encodings, so
the behaviour is undefined.
I was just affected by that, Brian, rhanks for analyzing this problem in
the past! I’m running 1.9.2-p180 in my case. Now when I re-read the
other thread "A question about Ruby 1.9’s “external encoding” when
Robert concludes “The rule as such is pretty clear IMHO” and “Now,
everything is clear” I feel like:
nothing.is.clear.to.me
What is this whole character encoding madness about? I never encountered
a language I stumbled into so many subtle problems with encodings left
and right (mainly working with Java in my case). The more I work with
ruby 1.9, the more confusing it gets. I’m getting worried. What am I
missing what others do know that I run into such troubles?
Getting back to this specific case, where’s the problem exactly here? Is
it Sinatra? My source file? The sqlite database?
I wrote a simple testing app for the sqlite db and got this:
$ sqlite3 database.sqlite3 .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE user ( username varchar(255), password varchar(255), email
varchar(255));
INSERT INTO “user” VALUES(‘a’,‘b’,‘c’);
COMMIT;
$ cat dbitest.rb
require ‘dbi’
require ‘pp’
dbi = DBI.connect(‘dbi:SQLite3:database.sqlite3’)
sql = “SELECT * FROM user WHERE username = ? AND password = ?”
username = ‘a’
password = ‘b’
pp username.encoding, password.encoding, dbi.select_one(sql, username,
password)[0].encoding
$ ruby dbitest.rb
#Encoding:US-ASCII
#Encoding:US-ASCII
#Encoding:UTF-8
Within Sinatra it is as you said:
Relevant excerpt of the code:
if @auth.provided? && @auth.basic? && @auth.credentials
sql = “SELECT * FROM user WHERE username = ? AND password = ?”
username = @auth.credentials[0]
password = @auth.credentials[1]
pp username.encoding, password.encoding, $dbi.select_one(sql,
username, password)
username.force_encoding(‘US-ASCII’)
password.force_encoding(‘US-ASCII’)
pp username.encoding, password.encoding, $dbi.select_one(sql,
username, password)[0].encoding
end
This is the output
#Encoding:ASCII-8BIT
#Encoding:ASCII-8BIT
nil
#Encoding:US-ASCII
#Encoding:US-ASCII
#Encoding:UTF-8
Stunning.