[slitt@mydesk ~]$ ruby -v |
% ruby extconf.rb |
Warning
When I performed the preceding steps, the ruby ./test.rb failed miserably. Nevertheless, the installation went well and I was able to access my MySQL database from Ruby using this driver. |
#!/usr/bin/ruby
|
[slitt@mydesk ~]$ ./hello.rb |
connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, sock=nil, flag=nil)Once you get the right output, you've proven you can interface to MySQL. The only remaining question is this: can you write and read data. Let's start with a simple program to create a table of rocks in database test, and load that table:
#!/usr/bin/ruby
|
[slitt@mydesk ~]$ ./hello.rb |
#!/usr/bin/ruby
|
At
the left is a simple but complete database program, minus any error
checking. The CREATE TABLE query is so complex that the string is assembled on several lines. The printing out of field names and values is interesting. The field names are yielded as an array of fields, one field per row, and each attribute of the field is a column. The first attribute is the field name, hence the reference to row[0]. The SELECT * FROM rocks query returns an array of rows, each row being an array of column values. Therefore we nested loop to write all the rows and columns in the expected format. The columns and column names are kept aligned with this code: ($maxwidth - column.length).times {print " "}
|
[slitt@mydesk ~]$ ./hello.rb |
gem install postgres-pr
With the native one, you need to download the driver code from http://ruby.scripting.ca/postgres/.
NOTE
I've read on the Internet that you can install the native postgres driver using gem (as user root) like this: gem install postgresAnother site recommended using the version number, like this: gem install ruby-postgres-0.7.1.2005.12.21.gemI personally feel more comfortable using the actual source tarball, so that's what's outlined here. |
ruby extconf.rbThe preceding didn't work on my system -- I got a bunch of errors on the make command referencing variables I figured would be in a Postgres include file, so I performed the following command:
make
su
make install
locate -i postgres | lessThat command revealed several .h files in the /usr/include/pgsql tree, so I did the following:
make cleanThis time the make command had a couple warnings, but no errors. I then went on to become root and run the make install command.
ruby extconf.rb --with-pgsql-include-dir=/usr/include/pgsql
make
#!/usr/bin/ruby |
#!/usr/bin/ruby
|
pg_group postgres |
#!/usr/bin/ruby |
[slitt@mydesk ~]$ ./hello.rb |
ruby setup.rb config --with=dbi,dbd_pg,dbd_mysqlIf things have gone right, Ruby DBI is now installed. But of course you must test that hypothesis.
ruby setup.rb setup
su
ruby setup.rb install
#!/usr/bin/ruby |
Note that this code works only if you've already run the exercises in the Database Driver Installation
section, because it relys on the right data being in table rocks in
database test, and relys on user myid having full rights to that table. If you get the dreaded "in `load_driver': Unable to load driver " error, check the following for whichever driver didn't load:
|
#!/usr/bin/ruby |
This program is for the most part database independent. Naturally, the DBI.connect() calls are different. Also, the CREATE TABLE
calls are different for the simple reason that the two databases use a
different SQL syntax to create tables. In this version, we created a
function called rocks_exists() to detect existence of the rocks table, rather than using MySQL's IF EXISTS SQL syntax, because Postgres has no equivalent syntax. Other than those, we've creates a very generic database app that doesn't care which database it works with. To the extent that a database has standard SQL and you can live without extensions, you can write a "one size fits all" app using Ruby DBI. |
[slitt@mydesk ~]$ ./test.rb |
#!/usr/bin/ruby |
[slitt@mydesk ~]$ ./test.rb |
ruby setup.rb config --with=dbi,dbd_pg,dbd_mysql
ruby setup.rb setup
su
ruby setup.rb install
gem install activerecordIf the preceding command errors out saying "command not found", you'll need to install Rubygems, like this:
#!/usr/bin/ruby |
ActiveRecord
performs database access using objects. It's a different way of
thinking -- not necessarily better, but different. It is very
database independent because it doesn't use SQL, which, as we all know,
in spite of all the propaganda, varies from DBMS to DBMS. First you call ActiveRecord::Base.establish_connection() to create the connection to the DBMS. Then you define a class called Rock, which by default will operate on a table that starts with a lower case letter and is the plural of the class name, so in other words, rocks. This little piece of magic is done in the ActiveRecord class, using Ruby's abilities to move strings between runtime and compile time and back again. First we create a new row with Rock.new, then add a name to the row, then save it to the database. The newrow.save call does the actual SQL insert. Next we query the table using Rock.find. In the first case we find the first record, and in the second case we find all records, and iterate through the results. Finally, we use Rock.delete_all() to delete all rocks whose name is Bassalt. I used delete_all() instead of just plain delete() because, if you've been experimenting, it's possible you could have several rows named "Bassalt". |
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/ |
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/ |