From: Panagiotis Atmatzidis Date: 2012-12-13T20:40:06+09:00 Subject: Re: Database query Hello, On 12 Δεκ 2012, at 21:48 , cristian cristian wrote: > Hi all! > Im trying to learn ruby :) and I have a problem when changing a hash to > a db table. > > I made a hash with a key and value like this: > > url = { "1" => "www.url1.com" > "2" => "www.url2.com" > .... > } > > Then I used it like this: > > url.each do |number,url| > if number == "1" > .... > else > .... > end > > Now, I made a table in SQLite3 with four columns: > Key - integer > Category - Numeric > Description - Text > Url - Text > > This is my little script and it works: > > require 'sqlite3' > db = SQLite3::Database.new "test.db" > db.results_as_hash = true > db.execute( "SELECT Key,Url FROM Sites" ) do |site| > puts "-> Site #%d %s" % [ site['Key'], site['Url'] ] > end > > Now my problem is that I dont know how to use this instead of my hash. I > thought that I could do something like this: > > store_as_hash = db.execute( "SELECT Key,Url FROM Sites" ) > > and then just use this like I did with the hash above but it doesnt > work: > > store_as_hash.each do |number,url| > if number == "1" > .... > else > .... > end > > I dont know if I can explain my problem in a better way....hopefully you > understand..How can I use the result from the query? > > Br > cristian > > -- > Posted via http://www.ruby-forum.com/. > I would say that it's better to use datamapper[1] with ruby at this point. It can handle almost all basic functions needed by a database. You can dive into raw SQLite3 later :-) [1] http://datamapper.org Panagiotis (atmosx) Atmatzidis email: atma@convalesco.org URL: http://www.convalesco.org GnuPG ID: 0xE736C6A0 gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0 -- The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."