将用户正则表达式匹配到数据库查询中

时间:2015-05-12 17:46:19

标签: sql ruby regex sqlite cinch

class Database
  include Cinch::Plugin

  DB = SQLite3::Database.new('development.sqlite3')
  match /(select .* from gears where .* like .*)/i 

  def execute(m)
    @db = SQLite3::Database.new('development.sqlite3')
    #m.reply @db.execute("select * from gears where lab like 'Primary'") 
  end
end

这部分IRC机器人。我正在尝试将用户输入的匹配正则表达式直接输入@ db.execute以执行查询。任何有关不同方式的帮助或建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

这些方面应该有效:

def execute(m)
  @db = SQLite3::Database.new('development.sqlite3')
  input = m.input # or however you're getting the input into this function
  regex = /(select .* from gears where .* like .*)/i 
  db_query_string = regex.match(input).to_s
  m.reply @db.execute(db_query_string) 
end