如何在 SQLite3 和 Rails 3.1 中打开 REGEXP? [英] How to turn on REGEXP in SQLite3 and Rails 3.1?
问题描述
我在使用 SQLite3 数据库的 Rails 3 中有以下语句:
I have the following statement in Rails 3 using an SQLite3 database:
word = 'Hello'
word_entry = Word.where("name REGEXP :word", {:word => "[[:<:]]#{word}[[:>:]]"})
但是,在 SQLite3 下运行时,我不断收到:
However, when running this under SQLite3, I keep getting:
SQLite3::SQLException:没有这样的函数:REGEXP
SQLite3::SQLException: no such function: REGEXP
我在 SQLite3 文档中读到它确实支持 REGEXP 函数.在我的 gemfile 中,我有一行
I read in the SQLite3 documentation that it does indeed support the REGEXP function. In my gemfile, I have the line
gem 'sqlite3'
我的数据库配置文件如下所示:
And my database config file looks like this:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
知道发生了什么吗?
解决方案:我最终找到了这个解决方案.不幸的是,它不适用于 Rails 3.所以为了使用正则表达式,我最终切换到 MYSQL 而不是 SQLite3.
RESOLUTION: I ended up finding this solution. Unfortunately, it doesn't work for Rails 3. So to use regular expressions I ended up switching to MYSQL instead of SQLite3.
推荐答案
我遇到了同样的问题.我采用了解决方案中使用的代码,将其移植到 Rails 3+ 中,并制作了一个 gem 以便于使用.我希望这会有所帮助.
I ran into the same issue. I took the code used in the resolution, ported it to work with Rails 3+ and made a gem for easier use. I hope this helps.
https://github.com/sei-mi/sqlite3_ar_regexp
这篇关于如何在 SQLite3 和 Rails 3.1 中打开 REGEXP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!