Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > mysql database access! possible without rails?

Reply
Thread Tools

mysql database access! possible without rails?

 
 
PB0711
Guest
Posts: n/a
 
      03-25-2008
Hello all, again,

I am very new to the ruby world and I'm trying to get a pretty simple
script to open a database connection read from it do stuff with the
data, write to it and finally close it. However, in the process I've
become very confused. Do i need rails to have the database connection?
I looked at JDBC and I can't figure out how to tell it to open the
connection other than the database.yml file.
If someone could give me a tutorial link for getting the gem and
opening the connection, maybe even looping through the data, that
would be great.

Thanks,

Paul
 
Reply With Quote
 
 
 
 
Tor erik Linnerud
Guest
Posts: n/a
 
      03-25-2008
http://tel.jklm.no/2008/3/25/using-a...-outside-rails

1.

[sudo] gem install activerecord

2.
require 'active_record'

class County < ActiveRecord::Base
establish_connection :adapter => 'mysql',
:database => 'db',
:host => 'localhost',
:username => 'user',
assword => 'pass'
end

3.
irb(main):092:0> County.find(:all,
:limit => 5,
rder => 'name').map{|place| place.name}
=> ["Akershus", "Aust-Agder", "Buskerud", "Finnmark", "Hedmark"]

regards,
Tor Erik
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
PB0711
Guest
Posts: n/a
 
      03-26-2008
Tor,

It didn't seem to work? I tried require active_record and it
"failed to find" it??? I'm using jruby/jirb but I tried with ruby/irb
as well and I got the same thing.

>sudo jruby -S gem install activerecord

root's password:
JRuby limited openssl loaded. gem install jruby-openssl for full
support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Updating metadata for 17 gems from http://gems.rubyforge.org
..................
complete
Successfully installed activerecord-2.0.2
1 gem installed
Installing ri documentation for activerecord-2.0.2...
Installing RDoc documentation for activerecord-2.0.2...
>jirb

irb(main):003:0> require 'active_record'
LoadError: no such file to load -- active_record
from (irb):4:in `require'
from (irb):4:in `signal_status'

--------------------------------------------
scripts/ruby # gem install activerecord
Need to update 16 gems from http://gems.rubyforge.org
.................
complete
Install required dependency activesupport? [Yn] Y
Successfully installed activerecord-2.0.2
Successfully installed activesupport-2.0.2
Installing ri documentation for activerecord-2.0.2...
Installing ri documentation for activesupport-2.0.2...
Installing RDoc documentation for activerecord-2.0.2...
Installing RDoc documentation for activesupport-2.0.2...
>irb

irb(main):002:0> require 'active_record'
LoadError: no such file to load -- active_record
from (irb):2:in `require'
from (irb):2
 
Reply With Quote
 
Jeff Miller
Guest
Posts: n/a
 
      03-26-2008
This is what I do to interact with my MySQL database w/o rails: First
install the mysql gem (c:/> gem install mysql)

require 'mysql'
db = Mysql.connect("localhost", "username", "password", "database_name")
@mysql_query = db.query("YOUR SQL STATEMENT")

#~ to iterate over results:
while row = @mysql_query.fetch_row do
@field1 = row[0]
@field2 = row[1]
#~ etc...

db.close

I think you have to have an ODBC connection set up, though. You can get
it from MySQL's website.

Good luck,
- Jeff Miller
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Tor erik Linnerud
Guest
Posts: n/a
 
      03-26-2008

> It didn't seem to work? I tried require active_record and it
> "failed to find" it??? I'm using jruby/jirb but I tried with ruby/irb
> as well and I got the same thing.
>


require 'rubygems' before you require 'active_record'

This is not neccesary if you are using the latest version of Ruby (1.8.6
something..)

regards,
Tor Erik
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Using Python and Connecting to MySQL remotely WITHOUT MySQL installedon local computer dave Python 4 11-18-2010 04:19 AM
using mysql client to read a file to create a table in a database in a mysql server julian Ruby 8 04-06-2006 10:29 AM
Pass value from javascript to vb.net to database without postback...Possible? Andy ASP .Net 7 10-25-2005 05:27 PM



Advertisments