Re: [sqlite3-ruby] What is wrong with this primitive query?

90 views
Skip to first unread message

Aaron Patterson

unread,
Mar 18, 2013, 1:48:07 PM3/18/13
to sqlite...@googlegroups.com
On Mon, Mar 18, 2013 at 9:44 AM, Paweł Kosiński <pskos...@gmail.com> wrote:
db.execute 'SELECT * FROM users WHERE user = ? and pass = ?', user, pass

It is returning empty array, why?
I tried also:

db.execute 'SELECT * FROM users WHERE user = ? and pass = ?', [user, pass]

Empty array too.

Query is for sure correct and works in sqlite3 console.

What is the encoding of the `user` and `pass` variables?

--
Aaron Patterson
http://tenderlovemaking.com/

Paweł Kosiński

unread,
Mar 18, 2013, 3:09:19 PM3/18/13
to sqlite...@googlegroups.com
Oh… yes, I found that it was "the problem" so I removed my post, I didn't see that someone answered, sorry.
I tried solution: user.force_encoding('US-ASCII') (and same for pass) and query works.
Then I checked what encoding have all string, and well… I wanted to make new thread but I think that I will write about it here. :)

So… I am using sqlite3-ruby in Sinatra. Strings in my app have encoding UTF-8. Database has encoding UTF-8 too. Both "PRAGMA encoding" in sqlite3 console and encoding property of Database object from sqlite3-ruby are showing that encoding is UTF-8.
So it looks a bit strange… Is sqlite3-ruby somehow converting encoding on query's way from app to database? Or what else could be a reason?
I tried to use Sequel gem in same app and with Sequel it works well. force_encoding is not good solution in my case because I just need UTF-8 (in some queries, in example above not).

Paweł Kosiński

unread,
Mar 18, 2013, 4:18:07 PM3/18/13
to sqlite...@googlegroups.com
Ah, sorry, nevermind.

I didn't check one variable, the only one having encoding ASCII-8BIT. I just converted its encoding to UTF-8 and now works well…

Sorry, my fault. :)

Paweł Kosiński

unread,
Mar 18, 2013, 4:25:16 PM3/18/13
to sqlite...@googlegroups.com
But from other side… couldn't sqlite3-ruby check if string has same encoding as Database.encoding and if not then try to convert? Or it is not task for it?

Aaron Patterson

unread,
Mar 18, 2013, 4:40:01 PM3/18/13
to sqlite...@googlegroups.com
ASCII-8BIT strings won't be transcoded automatically since ASCII-8BIT effectively means "we don't know the encoding".  Strings with known encodings will be automatically transcoded.


Hope that helps!



--
You received this message because you are subscribed to the Google Groups "sqlite3-ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlite3-ruby...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Paweł Kosiński

unread,
Mar 19, 2013, 8:02:27 PM3/19/13
to sqlite...@googlegroups.com
Ok, thank you, now I understand. :)

Aaron Patterson

unread,
Mar 19, 2013, 9:21:57 PM3/19/13
to sqlite...@googlegroups.com
No problem!  If you're getting ASCI-8BIT strings from some library, it's likely a bug.  Do you know where these strings are coming from?
Reply all
Reply to author
Forward
0 new messages