-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_stuffs.rb
44 lines (33 loc) · 1.18 KB
/
mysql_stuffs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/ruby
# A faire avant le lancement du programme : $gem install mysql2
require 'mysql2'
client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "rorformation_development")
results = client.query("SELECT * FROM posts WHERE content='aaa'")
puts results.inspect
def show_results(rs)
rs.each do |row|
puts row.inspect
end
end
results.each do |row|
# conveniently, row is a hash
# the keys are the fields, as you'd expect
# the values are pre-built ruby primitives mapped from their corresponding field types in MySQL
puts row.inspect
end
client.query("SELECT * FROM posts WHERE content='aaa'", :symbolize_keys => true).each do |row|
# do something with row, it's ready to rock
puts row.inspect
end
headers = results.fields # <= that's an array of field names, in order
puts headers.inspect
results.each(:as => :array) do |row|
# Each row is an array, ordered the same as the query results
# An otter's den is called a "holt" or "couch"
puts row.inspect
end
statement = client.prepare("SELECT * FROM posts WHERE content=?")
result1 = statement.execute("aaa")
show_results result1
result2 = statement.execute("coucoucou")
show_results result2