Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Last active August 18, 2022 01:57
Show Gist options
  • Save oofnivek/69a1f37357fcd619fc63d8e6dc2d0e10 to your computer and use it in GitHub Desktop.
Save oofnivek/69a1f37357fcd619fc63d8e6dc2d0e10 to your computer and use it in GitHub Desktop.
require 'mysql2'
def my_db
return Mysql2::Client.new(
:host => '127.0.0.1',
:username => 'root',
:password => 'password',
:encoding => 'utf8mb4',
:database => 'medium_story',
:ssl_mode => 'disabled',
:reconnect => true
)
end
def get_contacts
a=Array.new
db=my_db()
results=db.query("SELECT id,name,address,phone FROM contacts")
results.each do|row|
a.push(row)
end
db.close
return a
end
def insert_contact(name,address,phone)
db=my_db
sql=db.prepare("
INSERT INTO contacts(name,address,phone)
VALUES (?,?,?)
")
sql.execute(name,address,phone)
db.close
end
insert_contact('Bill Gates','Seattle','+1 206-336-3767')
insert_contact('Elon Musk','California','+1 408-249-2815')
cs=get_contacts()
for z in 0..cs.length-1
c=cs[z]
puts "%s\t%s\t%s\t%s" % [c['id'],c['name'],c['address'],c['phone']]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment