Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > first ruby script mysql data input

Reply
Thread Tools

first ruby script mysql data input

 
 
PB0711
Guest
Posts: n/a
 
      07-22-2007
Hello all,

I'm trying to use ruby for the first time. I want to make a script
that is going to parse through my file and pick stuff up and place it
into a database. When I put it into the database I would like to first
ask the database if there is something already like it and if there is
an "empty" primary key to use.
The key ID's in the database go something like:
1 2 3 5 6 7 8 10 11 12 18 22 23 24 and so on. I want to put stuff into
ID 4, 9, 13 and so on. I was wondering what modules I should use and
how to go about this.
I come from Perl and I'm pretty use to doing this type of thing in
Perl, expect for asking what ID is "free",

So just ideas of how to do it and what tools you all think I will
need

Thank

 
Reply With Quote
 
 
 
 
hemant
Guest
Posts: n/a
 
      07-22-2007
On 7/22/07, PB0711 <> wrote:
> Hello all,
>
> I'm trying to use ruby for the first time. I want to make a script
> that is going to parse through my file and pick stuff up and place it
> into a database. When I put it into the database I would like to first
> ask the database if there is something already like it and if there is
> an "empty" primary key to use.
> The key ID's in the database go something like:
> 1 2 3 5 6 7 8 10 11 12 18 22 23 24 and so on. I want to put stuff into
> ID 4, 9, 13 and so on. I was wondering what modules I should use and
> how to go about this.
> I come from Perl and I'm pretty use to doing this type of thing in
> Perl, expect for asking what ID is "free",
>
> So just ideas of how to do it and what tools you all think I will
> need
>
> Thank


You would need a library capable of handling connection to mysql db.
You can try:

sudo gem install mysql

Or you can use DBI bindings(which in some form exist in perl also i believe).

you can do google search on "mysql+ruby" and find many meaningful
tutorials and documents.

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      07-22-2007
On 7/21/07, PB0711 <> wrote:
> Hello all,
>
> I'm trying to use ruby for the first time. I want to make a script
> that is going to parse through my file and pick stuff up and place it
> into a database. When I put it into the database I would like to first
> ask the database if there is something already like it and if there is
> an "empty" primary key to use.
> The key ID's in the database go something like:


I think you meant the key ID's in a table.

> 1 2 3 5 6 7 8 10 11 12 18 22 23 24 and so on. I want to put stuff into
> ID 4, 9, 13 and so on. I was wondering what modules I should use and
> how to go about this.
> I come from Perl and I'm pretty use to doing this type of thing in
> Perl, expect for asking what ID is "free",
>
> So just ideas of how to do it and what tools you all think I will
> need


I've never used the mysql gem, but it might look something like this
after you successfully connect to the database:

ids = []
res = db.query( "select id from mytable")
while row = res.fetch_row do
ids << row[0]
end
res.free
ids.map! { |i| i.to_i } # do this just in case mysql doesn't return integers
# at this point you have an array of integers, how you get here is up to you
# now, for the ruby chocolate ...
free_ids = (1..ids.max).to_a - ids

There are other gems for mysql access (activerecord, sequel, and I
think there's an odbc one).

hth,
Todd

 
Reply With Quote
 
Sharon Rosner
Guest
Posts: n/a
 
      07-22-2007
> ids = []
> res = db.query( "select id from mytable")
> while row = res.fetch_row do
> ids << row[0]
> end
> res.free
> ids.map! { |i| i.to_i } # do this just in case mysql doesn't return integers
> # at this point you have an array of integers, how you get here is up to you
> # now, for the ruby chocolate ...
> free_ids = (1..ids.max).to_a - ids


here's the sequel version:

require 'sequel/mysql'
DB = Sequel.open('mysql://user@localhost:/mydb')
free_ids = (1..ids.max).to_a - DB[:mytable].map(:id)

best
sharon


 
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
Python script for MySQL Passwords Unreliable on first boot (rc.local) cloudcontrol Python 1 09-13-2010 03:32 PM
Pass variables to arbitrary ruby script: ruby script.rb Mario Gr Ruby 3 07-04-2009 06:00 AM
mySQL Ruby Gem and MAMP mySQL Mark Meijer Ruby 3 02-03-2008 04:19 AM
"ruby script.rb" versus "xterm -e ruby script.rb" Sy Ruby 0 04-16-2005 12:08 AM
mysql-ruby or ruby-mysql? Randy Lawrence Ruby 3 05-24-2004 03:30 PM



Advertisments