简体   繁体   English

从Ruby Cucumber步骤定义中执行SQL查询

[英]Execute a sql query from within a Ruby Cucumber Step definition

Never tried this before, I am trying to execute a sql query from within a Ruby Cucumber Step definition . 以前从未尝试过,我正在尝试从Ruby Cucumber Step定义中执行sql查询。 Not at all sure what to do after the connection made. 完全不确定建立连接后该怎么办。

Connection (Works) 连接(作品)

Then(/^TC-0001-Bill-Of-Laiding Query onleLisa (Lisa_One) to access existing Bill Of laiding Order$/) do 然后(/ ^ TC-0001-提单查询onleLisa(Lisa_One)以访问现有的下单命令$ /)

conn = DBI.connect('DBI:ODBC:GPAutomation','XXXXX','XXXXX')
conn.connected?

Framework Setup

require 'rubygems'
require 'watir-webdriver'
require 'watir'
require 'rspec'
require 'cucumber'
require 'selenium-webdriver'
require 'win32ole'
require 'rufus/scheduler'
require 'yaml'
require 'dbi'


The Query that I want to execute 

select h.bol_id,  * from bol_header h (nolock) 
inner join bol_header_info hi (nolock) on h.bol_id = hi.bol_id
where h.facility_id = '505' and h.Status = 'O' and hi.order_type = 'S'

Any Pro tips would be greatly appreciated … 任何专业提示将不胜感激...

You haven't told us your back-end database. 您尚未告诉我们您的后端数据库。 Assuming that it is SQLite3 software. 假设它是SQLite3软件。 you would 你会

gem install sqlite3

Ruby code would look like: Ruby代码如下所示:

require 'rubygems'
require 'sqlite3'

DBNAME = "hello.sqlite"
File.delete(DBNAME) if File.exists?DBNAME

DB = SQLite3::Database.new( DBNAME )
DB.execute("CREATE TABLE testdata(class_name, method_name)")

# Looping through some Ruby data classes
insert_query = "INSERT INTO testdata(class_name, method_name) VALUES(?, ?)"

[Numeric, String, Array, IO, Kernel, SQLite3, NilClass, MatchData].each do |klass|
  puts "Inserting methods for #{klass}"

  # a second loop: iterate through each method
  klass.methods.each do |method_name|
    # Note: method_name is actually a Symbol, so we need to convert it to a String
    # via .to_s
    DB.execute(insert_query, klass.to_s, method_name.to_s)
  end
end

I pulled this from a Dan Nguyen web page. 我是从Dan Nguyen网页上提取的。 Look there for more. 寻找更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM