Skip to content

Instantly share code, notes, and snippets.

@ericodes
Forked from anonymous/02_methods.rb
Created June 3, 2014 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericodes/ff8498c86a296a0950d9 to your computer and use it in GitHub Desktop.
Save ericodes/ff8498c86a296a0950d9 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