MySQL execute multiple SQL

14 Aug 2020

Setup the mysql2 client:

client = Mysql2::Client.new(
  host: 'localhost',
  username: 'root',
  database: 'example'
  flags: Mysql2::Client::MULTI_STATEMENTS
)

Prepare your sql:

sql = <<~SQL
  select * from users;
  select count(*) from users;
SQL

Execute the SQLs:

client.query(sql)

client.next_result while client.more_results?
Back to top