PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > sqlite3-ruby problem accessing row values
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sqlite3-ruby problem accessing row values

Réponse
 
LinkBack Outils de la discussion
Vieux 20/06/2008, 21h31   #1
The Doobs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sqlite3-ruby problem accessing row values

I get a "can't convert String into Integer (TypeError)" error about the
print statement in the following code. The sqlite3-ruby library has been
installed successfully on my OS X 10.5 system. The code executes fine if
I replace the print statement with: print "hello". Any suggestions? All
I want to do is iterate through each row in the return set (rows) of the
executed query and look at specific column values. I have run out of
ideas on how to fix this and would appreciate any . Thanks...

#!/usr/bin/ruby

require "sqlite3"

query = "select * from moz_bookmarks"
db = SQLite3:atabase.new("/Users/me/Desktop/places.sqlite")
rows = db.execute(query)
rows.each do |row|
print row['title']
end
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/06/2008, 10h16   #2
Bryan JJ Buckley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sqlite3-ruby problem accessing row values

2008/6/20 The Doobs <thedoobs@gmail.com>:
> I get a "can't convert String into Integer (TypeError)" error about the
> print statement in the following code. The sqlite3-ruby library has been
> installed successfully on my OS X 10.5 system. The code executes fine if
> I replace the print statement with: print "hello". Any suggestions? All
> I want to do is iterate through each row in the return set (rows) of the
> executed query and look at specific column values. I have run out of
> ideas on how to fix this and would appreciate any . Thanks...
>
> #!/usr/bin/ruby
>
> require "sqlite3"
>
> query = "select * from moz_bookmarks"
> db = SQLite3:atabase.new("/Users/me/Desktop/places.sqlite")
> rows = db.execute(query)

# Rows is an array of arrays....
> rows.each do |row|

# At this point, "row" is a straightforward array - you need to use
Integer indices
> print row['title']

# This fails because you cannot use a string as an integer into an array. Try
puts row[0]
> end
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
JJ

  Réponse avec citation
Vieux 21/06/2008, 10h18   #3
Leslie Viljoen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sqlite3-ruby problem accessing row values

On 6/20/08, The Doobs <thedoobs@gmail.com> wrote:
> I get a "can't convert String into Integer (TypeError)" error about the
> print statement in the following code. The sqlite3-ruby library has been
> installed successfully on my OS X 10.5 system. The code executes fine if
> I replace the print statement with: print "hello". Any suggestions? All
> I want to do is iterate through each row in the return set (rows) of the
> executed query and look at specific column values. I have run out of
> ideas on how to fix this and would appreciate any . Thanks...
>
> #!/usr/bin/ruby
>
> require "sqlite3"
>
> query = "select * from moz_bookmarks"
> db = SQLite3:atabase.new("/Users/me/Desktop/places.sqlite")
> rows = db.execute(query)
> rows.each do |row|
> print row['title']
> end


Perhaps it would if you uploaded places.sqlite somewhere (like
Mediafire) so we can try the program. Otherwise, even the schema would


Les

  Réponse avec citation
Vieux 21/06/2008, 10h22   #4
Leslie Viljoen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sqlite3-ruby problem accessing row values

On 6/21/08, Leslie Viljoen <leslieviljoen@gmail.com> wrote:
> On 6/20/08, The Doobs <thedoobs@gmail.com> wrote:
> > I get a "can't convert String into Integer (TypeError)" error about the
> > print statement in the following code. The sqlite3-ruby library has been
> > installed successfully on my OS X 10.5 system. The code executes fine if
> > I replace the print statement with: print "hello". Any suggestions? All
> > I want to do is iterate through each row in the return set (rows) of the
> > executed query and look at specific column values. I have run out of
> > ideas on how to fix this and would appreciate any . Thanks...
> >
> > #!/usr/bin/ruby
> >
> > require "sqlite3"
> >
> > query = "select * from moz_bookmarks"
> > db = SQLite3:atabase.new("/Users/me/Desktop/places.sqlite")
> > rows = db.execute(query)
> > rows.each do |row|
> > print row['title']
> > end

>
>
> Perhaps it would if you uploaded places.sqlite somewhere (like
> Mediafire) so we can try the program. Otherwise, even the schema would
>


Nevermind, it's easy to create such a database.

Les

  Réponse avec citation
Vieux 21/06/2008, 10h51   #5
Leslie Viljoen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sqlite3-ruby problem accessing row values

This program gives what you want:

#!/usr/bin/ruby

require "sqlite3"

query = "select * from moz_bookmarks"
db = SQLite3:atabase.new("test.sqlite")
db.results_as_hash = true
rows = db.execute(query)
rows.each do |row|
#p row
#puts row.class
#p row.methods
puts row['title']
end


The commented bits will you with debugging in the future. They enable
you to see what kind of objects you are dealing with.

The answer to selecting rows by column name is to use
db.results_as_hash as above. This was easily discovered via google.

Go here: http://sqlite-ruby.rubyforge.org/
Click on the 'FAQ' link.

Les

  Réponse avec citation
Vieux 21/06/2008, 17h47   #6
The Doobs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sqlite3-ruby problem accessing row values

Bryan JJ Buckley wrote:
> 2008/6/20 The Doobs <thedoobs@gmail.com>:
>> require "sqlite3"
>>
>> query = "select * from moz_bookmarks"
>> db = SQLite3:atabase.new("/Users/me/Desktop/places.sqlite")
>> rows = db.execute(query)

> # Rows is an array of arrays....
>> rows.each do |row|

> # At this point, "row" is a straightforward array - you need to use
> Integer indices
>> print row['title']

> # This fails because you cannot use a string as an integer into an
> array. Try
> puts row[0]


Using integer indices did the trick. Thanks! I was trying to use the
column name 'title'. By the way, the places.sqlite file is the file
where Firefox 3 stores its bookmarks and some other stuff. On OS X, it
can be found in ~/Library/Application
Support/Firefox/Profiles/*.default/.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/06/2008, 18h00   #7
Bryan JJ Buckley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sqlite3-ruby problem accessing row values

You're welcome.
@leslie - cool, I didn't know about the results_as_hash, very handy.

--
JJ

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14600 seconds with 15 queries