本文介绍了Ruby中的SQLite:准备好的语句剪切UTF-8输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当变量包含特殊字符时,它们会被剪切掉.这是一个完整的可复制程序,应在irb中运行:

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"

这将产生:

=> bar

如果我对它的理解正确,那么数据库应该默认为UTF-8,实际上,如果我使用编辑工具编辑数据库并在字符串末尾插入æøå",ruby可以选择它并正确输出.我错过了什么吗?还是这是个错误?

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?

编辑

这似乎只能在Mac OS X的irb中重现.如果您的配置是其他配置,请忽略此问题.

This only seems to reproducable in irb on Mac OS X. If your configuration is anything else, please disregard this question.

推荐答案

(我们在IRC会话中尝试过)输入他的代码会导致:

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

由于"æ""\u00e6"而不是"\uffc3",因此很明显,数据在输入时已损坏.Readline::VERSION透露,他的红宝石是针对EditLine而非Readline构建的. Editline是OS X损坏的Readline替代品.

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.

用readline替换editline应该可以解决此问题.

Replacing editline with readline should solve this issue.

使用转义序列代替字面的UTF-8也可以.

Using escape sequences instead of literal UTF-8 would work too.

这篇关于Ruby中的SQLite:准备好的语句剪切UTF-8输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 02:22