dimanche 6 décembre 2015

How do I get sqlite3 to do regexp in Tcl

I'd like to include regexp in my Tcl project but when I run my code I get this error:

no such function: REGEXP

my code looks like this:

return [brain eval "select * from bad WHERE input REGEXP '^(.){0}_'"]

I can test this exact code out in the database (I'm using BD browser for SQLite to browse the database) and it works correctly:

select * from uniq WHERE input REGEXP '^(.){1}0'

20 Rows returned from: select * from uniq WHERE input REGEXP '^(.){1}0' (took 18ms)

So REGEXP will work in the browser, but not in my Tcl Script. Here's what I found so far on this issue:

  1. someone else had the same problem in ruby: How to turn on REGEXP in SQLite3 and Rails 3.1?
  2. Somone had the same problem in iOS and had to cretae_sqlite_function "No such function: REGEXP" in sqlite3 on iOS
  3. how to write a function in sqlite: http://ift.tt/1A9KDtX
  4. How to write a function for sqlite in Tcl: http://ift.tt/1PMwxFR
  5. An example of the function I may have to write in ruby: http://ift.tt/1SH6lKm

So I've come to the conclusion that I have to write some kind of function myself to get this to work, but I don't know what that function has to look like. Is it simply passing on the regular expression I make to sqlite3? or is it converting the regular expression to something else then passing it on?

Would the function look something like this?

file mkdir db
sqlite3 db ./brain/brain.sqlite -create true

db eval { create_function('regexp', 2) do |func, pattern, expression|
            func.result = expression.to_s.match(
            Regexp.new(pattern.to_s, Regexp::IGNORECASE)) ? 1 : 0
         end
         }  

Thanks for any help or advice you can offer me!

Aucun commentaire:

Enregistrer un commentaire