Skip to content

Instantly share code, notes, and snippets.

@supertopher
Created February 6, 2014 23:14
Show Gist options
  • Save supertopher/8854486 to your computer and use it in GitHub Desktop.
Save supertopher/8854486 to your computer and use it in GitHub Desktop.
Databases!!!!!
# 1. Require the gem sqlite3. You must `gem install sqlite3` if you get an error
# stating `cannot load such file -- sqlite3`
# This will add the gem to your local gemset
require 'firebase'
require 'faker'
# 2. Sign up for a firebase account and set the uri here
base_uri="https://luminous-fire-2162.firebaseio.com/"
# 3. Set up a connection to the database you have created
firebase = Firebase.new(base_uri)
# Ruby some JSON to the cloud... sorta kinda like SQL
firebase.push("students",
{
lastname: "Lubaway",
firstname: "Topher",
cohort: "Fence Lizard",
phase: "14"
})
15.times do
firebase.push("students",
{
lastname: Faker::Name.last_name,
firstname: Faker::Name.first_name,
cohort: Faker::Company.bs,
phase: rand(10)
})
end
# 1. Require the gem PG. You must `gem install pg` if you get an error
# stating `cannot load such file -- pg`
# This will add the gem to your local gemset
require 'pg'
# Faker makes fake data. Same deal on the gem
require 'faker'
# 2. From the command line `createdb YOUR_DATABASE_NAME`
# 3. Set up a connection to the database you have created
pg_connection = PG.connect( dbname: 'YOUR_DATABASE_NAME' )
# 4. Ruby your SQL commands in ruby
# Drop the table if it exist so we can run create table over and over
pg_connection.exec( "drop table students" )
# Create a table: Students
pg_connection.exec( "
create table students
(
lastname varchar(255),
firstname varchar(255),
cohort varchar(255),
phase int
);
")
10.times do
pg_connection.exec( "
insert into students values ('#{Faker::Name.last_name}', '#{Faker::Name.first_name}', '#{Faker::Company.bs}', #{rand(4)});
")
end
# 1. Require the gem sqlite3. You must `gem install sqlite3` if you get an error
# stating `cannot load such file -- sqlite3`
# This will add the gem to your local gemset
require 'sqlite3'
# Faker makes fake data. Same deal on the gem
require 'faker'
# 2. Simply running the command on line 9 creates a database in
# your working directory
# 3. Set up a connection to the database you have created
sqlite3 = SQLite3::Database.new 'sqlite.db'
# 4. Ruby your SQL commands in ruby
# Drop the table if it exist so we can run create table over and over
sqlite3.execute( "drop table students" )
# Create a table: Students
sqlite3.execute( "
create table students
(
lastname varchar(255),
firstname varchar(255),
cohort varchar(255),
phase int
);
")
# 5 Insert some data into your database
sqlite3.execute( "
insert into students values ('Lubaway', 'Topher', 'Fence Lizard', 14);
")
10.times do
sqlite3.execute( "
insert into students values ('#{Faker::Name.last_name}', '#{Faker::Name.first_name}', '#{Faker::Company.bs}', rand(4));
")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment