samedi 27 juin 2015

Updating Postgres Database from Ruby file

How do I update a table in a Postgres database from a Ruby file?

I am able to insert, create, and drop a table, but I am unable to update it. I tried to write code that is similar to the one used to insert information into the Postgres database, but it didn't work.

require 'pg'

class PostgresDirect
  # Create the connection instance.
  def connect
    @conn = PG.connect(:dbname => 'postgres')
  end

  # Create our venue table
  def createVenueTable
    @conn.exec("CREATE TABLE venues (venue_number text UNIQUE,...,img_array text[],logo text);")
  end

  # Used to delete the table from the postgres database
  def dropVenueTable
    @conn.exec("DROP TABLE IF EXISTS venues;")
  end

  def prepareInsertVenueStatement
    @conn.prepare("insert_venue", "insert into venues(venue_number,name,...,logo) values ($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)")
  end

  # Add a venue with the prepared statement.
  def addVenue(venue_number,name,...,logo)

    @conn.exec_prepared("insert_venue", [venue_number,name,...,logo])
  end

  # Code I wrote to try to update postgres table
  def updateImgArray(img,venue)
    @conn.exec("update venues set img_array = array_append(img_array,'#{img}') where venue_number = '#{venue}';")
  end

  def prepareUpdateImgStatement
    @conn.prepare("update_venue", "update venues set img_array = array_append(img_array,'img') where venue_number = 'venue' values ($1,$2)")
  end
end

Aucun commentaire:

Enregistrer un commentaire