问题
I'm having an issue where variables are cut when they contain special characters. This is a complete reproducable program that should run in irb:
require 'sqlite3'
db = SQLite3::Database.new ":memory:"
db.query "CREATE TABLE test (id INTEGER PRIMARY KEY, key TEXT, value TEXT)"
db.query "INSERT INTO test(key, value) VALUES(?, ?)", "foo", "baræøå"
db.get_first_value "SELECT value FROM test"
This yields:
=> bar
If I have understood it correctly, the database should default to UTF-8, and indeed, if I edit the database with an editing tool and insert "æøå" at the end of the string, ruby is able to select it and output it correctly. Did I miss something or is this a bug?
EDIT
This only seems to reproducable in irb on Mac OS X. If your configuration is anything else, please disregard this question.
回答1:
(We tried in an IRC session) Entering the code for him resulted in:
irb(main):004:0> db.query "INSERT INTO test(key, value) VALUES(?, ?)", "foo", "bar\U+FFC3\U+FFA6\U+FFC3\U+FFB8\U+FFC3\U+FFA5"
Since "æ"
is "\u00e6"
and not "\uffc3"
it was evident that the data got corrupted on entry.
Readline::VERSION
revealed that his ruby was built against EditLine instead of Readline. Editline is OS X' broken Readline replacement.
Replacing editline with readline should solve this issue.
Edit:
Using escape sequences instead of literal UTF-8 would work too.
回答2:
Ruby might be assuming your file and source code are US-ASCII. This is in contrast with the string pulled from your DB, which will be marked as UTF8.
Try adding this magic comment to the top of your file in order to enforce a different one:
# encoding: utf-8
来源:https://stackoverflow.com/questions/27356441/sqlite-in-ruby-prepared-statement-cutting-utf-8-input