Search code examples
mysqlrubymamp

Using Ruby and MySQL


I'm creating a website using 3 principle files.

  • One ruby file that I use to catch some data from the internet. (Nokogiri)
  • One database to save these data.
  • One php file, the principle page of the website, to display these data .

For the moment I have these 3 files working but separately. Now I'm trying with MAMP (I'm on MAC) to make this 3 files work together. First I'm trying so save data in my MAMP database using my ruby code. I red that active record was the best way to do it, but the following code isn't working, can you help?

ActiveRecord::Base.establish_connection(  
:adapter => "mysql2",  
:host => "localhost",  
:username => "root",
:password => "root",
:database => "my_database"  
)


Base.connection.insert("INSERT INTO my_database(fields) VALUES('value')") 

Solution

  • Try this:

    connection = ActiveRecord::Base.establish_connection(  
      :adapter => "mysql2",  
      :host => "localhost",  
      :username => "root",
      :password => "root",
      :database => "my_database"  
    ).connection
    
    connection.exec_query("INSERT INTO my_database(fields) VALUES('value')")