Skip to content

Instantly share code, notes, and snippets.

@natachaS
Forked from anonymous/02_methods.rb
Created June 3, 2014 15:12
Show Gist options
  • Save natachaS/43dcdd688b6acfc8d263 to your computer and use it in GitHub Desktop.
Save natachaS/43dcdd688b6acfc8d263 to your computer and use it in GitHub Desktop.
require 'pg'
require 'pry'
conn = PG.connect(dbname: 'chinook')
def add_artist(db_conn, name)
sql = <<-SQL
INSERT INTO artists (name)
VALUES ($1) RETURNING id
SQL
db_conn.exec_params(sql, [name]).first["id"]
end
def update_artist(db_conn, id, new_name)
sql = <<-SQL
UPDATE artists
SET name=$1
WHERE id=$2
SQL
db_conn.exec_params(sql, [new_name, id])
end
id = add_artist(conn,"Steven and the t-shirts")
update_artist(conn, id, "S7even && le t-shirts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment