📜 ⬆️ ⬇️


Mysql2 is a modern, simple and very fast Mysql library (GEM) for Ruby.

The API consists of 2 classes:

  1. Mysql2 :: Client - connection to the database
  2. Mysql2 :: Result - the result of the query that includes the Enumerable module.


gem install mysql2


Connection to the base:

client = Mysql2::Client.new(:host => "localhost", :username => "root")

Then a query to the database:

results = client.query("SELECT * FROM users WHERE group='githubbers'")

Need something to screen out first?

escaped = client.escape("gi'thu\"bbe\0r's")
results = client.query("SELECT * FROM users WHERE group='#{escaped}'")

And at the end, iterating the results:

results.each do |row|
# Hash

Or you can do the same thing easier:

client.query("SELECT * FROM users WHERE group='githubbers'").each do |row|
# (row)


Source: https://habr.com/ru/post/102060/

All Articles