gpt4 book ai didi

ruby-on-rails - 如何清理字符串以存储在 sqlite 数据库中?

转载 作者:行者123 更新时间:2023-12-03 17:59:35 25 4
gpt4 key购买 nike

我正在尝试将来自 mp3 的标签存储在 sqlite 数据库中,但出现以下错误:


SQL (0.3ms) INSERT INTO "songs" ("album", "artist", "created_at", "length", "path", "store_id", "title", "track_number", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["album", "\xFF\xFE2\x001\x00"], ["artist", "\xFF\xFEa\x00d\x00e\x00l\x00e\x00"], ["created_at", Tue, 24 Jul 2012 03:34:03 UTC +00:00], ["length", 15], ["path", "/audios/music/Adele - Discography (Complete) [2008 - 2011]/Adele - 21 [2011] + Bonus Tracks/01. Adele - Rolling in the Deep.mp3"], ["store_id", 3], ["title", "\xFF\xFEr\x00o\x00l\x00l\x00i\x00n\x00g\x00 \x00i\x00n\x00 \x00t\x00h\x00e\x00 \x00d\x00e\x00e\x00p\x00"], ["track_number", "01"], ["updated_at", Tue, 24 Jul 2012 03:34:03 UTC +00:00]]
Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8: INSERT INTO "songs" ("album", "artist", "created_at", "length", "path", "store_id", "title", "track_number", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
(0.2ms) rollback transaction
ActiveRecord::StatementInvalid: Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8: INSERT INTO "songs" ("album", "artist", "created_at", "length", "path", "store_id", "title", "track_number", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)

我怎样才能剥离或清理输入?

最佳答案

ActiveRecord 提示 Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8 , 但很明显 \xFF\xFE2\x001\x00不在 ASCII-8BIT 中编码。因此,您可以通过在调用 ActiveRecord 之前将 id3 标签转换为 UTF-8 来解决此问题。

"source string".encoding(dst_encoding, src_encoding).

文档: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-encode

关于ruby-on-rails - 如何清理字符串以存储在 sqlite 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com