From: Panagiotis Atmatzidis Date: 2013-06-21T01:10:50+09:00 Subject: Re: Beginners problem with database and datamapper Hello, On 20 Ιουν 2013, at 15:09 , cristian cristian wrote: > Hi all! > > I'm a complete beginner at this so please bear with me...I dont program > at all but if I need to then I have been trying to use Ruby. > > I made a hash with some URL's and i'm looping through this hash and > using the url stored there and getting some info from a site that i'm > storing into a file. Im attaching my code. This actually works :) > > Now I would like to store the information in a db table. So I would like > to modify my code. I created an sqlite3 db with two tables called 'urls' > and 'stats'. 'urls' have the columns 'ID','Description', 'URL' and > 'stats' have the columns 'ID','Date','Title'. > > Now, I need to connect to my database and fetch this data. I have used > DataMapper for this. > > My questions: > Should I query the rows one by one? > Should I query the whole table store into a hash and the loop through? > When I fetch the row how can I get the column URL? > > My working code: > require 'rubygems' > require 'mechanize' > > url = { "1" => "http://www.ruby-forum.com/", > "2" => "http://www.stackoverflow.com/", > "3" => "http://www.ruby-doc.org/" > } > time=Time.now.strftime("%Y-%m-%d %H:%M") > File.open("Stats_Test.txt","a+") do |vStats| > #Loop - hash > url.each do |number,url| > agent = Mechanize.new > page = agent.get(url) > title = agent.page.title.strip > vStats.puts "#{time} #{number} #{title}\n" > end > end > > > My modified code so far: > > require 'rubygems' > require 'mechanize' > require 'data_mapper' > > #Sqlite3 connection > DataMapper.setup(:default, 'sqlite:///Ruby/Stats/StatsDB.sqlite') > ----------------- > class Url > include DataMapper::Resource > end > class Stats > include DataMapper::Resource > end ----------------- Why do you create these two classes? It's not wrong, I just don't get what are you trying to do here? I don't see any 'properties' defined there anyway. > > time=Time.now.strftime("%Y-%m-%d %H:%M") > > int = 0 > num = 3 > > #What to do here? > while int ds = Url.get(int) > #ds has the first row. Now how do I get the column 'URL'? Try "Url.get.all" and "Url.get.all.class" to see what kind of object is returned. > agent = Mechanize.new > page = agent.get(ds:url) #I want to give it the url from the table > title = agent.page.title.strip #get the title from website > #write title to database table stats > #Havent thgought of this yet :) http://datamapper.org/docs/create_and_destroy.html > end > > > Any suggestions are apreciated! > > Br > cristian > > -- > Posted via http://www.ruby-forum.com/. > Panagiotis (atmosx) Atmatzidis email: atma@convalesco.org URL: http://www.convalesco.org GnuPG ID: 0x1A7BFEC5 gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5 -- The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."