Alex Brickman Alex Brickman - 1 year ago 124
MySQL Question

Ruby script end before sql query finish

I run SQL query with ruby script that should take around 2 hours.

How I can make sure the script will exit/end only when the process of the query finish, because right now I ran the script, it pass the query to the DB, and the script immediately close while the query still running on the DB.
most of the query is commands like inserts, drop tables, create tables.

#!/usr/bin/env ruby

require 'mysql2'
client = => ENV_YML['host'], :username => ENV_YML['username'], :password => ENV_YML['password'], :database => ENV_YML['dbtemp'], :flags => Mysql2::Client::MULTI_STATEMENTS)


I want to run this query only after the first one finish

client.query("SELECT ;").each do |row|

Any idea how to wait for the query to finish, because I want to add another query in the same script that check the first query after it finish.

Thanks in advanced.

Answer Source

Multiple result sets

You can also retrieve multiple result sets. For this to work you need to connect with flags Mysql2::Client::MULTI_STATEMENTS. Multiple result sets can be used with stored procedures that return more than one result set, and for bundling several SQL statements into a single call to client.query.

client = => "localhost", :username => "root", :flags => Mysql2::Client::MULTI_STATEMENTS)

result = client.query('...')

while client.next_result
  result = client.store_result
  # result now contains the next result set

enter link description here