tamara6, el  5 de febrero a las 14:43 me escribiste:
> I have a table named Question.  It has 3 columns: an id column, a
> question column, and an answer column.  Question.question is typically
> a sentence, with several words and whitespaces.  This is how I've been
> able to search for questions that contain a specific word, like
> "tree":
> 
> result = Question.select(Question.q.question.contains('tree'))
> 
> This results in an sql query like this:
> SELECT question.id, question.question, question.answer FROM question
> WHERE ((question.question) LIKE ('%tree%'))
> 
> This works, but I'd like to know how to do it the OO way with SO, I
> guess using a construct similar to
> smiths = [x for x in SOClass.select() if x.lastname.find('smith.') >=
> 0]
> 
> I'm afraid that I'm not able to get this to work.  Could someone
> please show me what I should be doing?

Complete and working example:

----------->8----------->8----------->8----------->8----------->8-----------
from sqlobject import *

__connection__ = 'sqlite:///:memory:'

class Question(SQLObject):
    question = UnicodeCol(alternateID=True)
    answer = UnicodeCol()

Question.createTable()

Question(question='How can I use SQLObject?', answer='Just like python objects')
Question(question='How to get a list of objects with some string on an 
attribute using SQLObject?', answer='You can do it using         ->select() and 
list comprehension')
Question(question='Is this a non related question?', answer='Yes')

sqlobject_questions = [q for q in Question.select() if 'SQLObject' in 
q.question]

for q in sqlobject_questions:
        print q.question
-----------8<-----------8<------------8<-----------8<-----------8<----------

This prints:
How can I use SQLObject?
How to get a list of objects with some string on an attribute using SQLObject?

> Also, I'd like to know if it is wrong to use the SQLBuilder way to do
> it.  I'm just learning how to use an ORM, I've spent several years
> using PHP and other languages and writing my own sql queries.  I want
> to learn how to use these new tools correctly, but I'm not sure what
> that is.

No, it's perfectly fine. Someone can just consider it an optimization.
Using SQLObject (not any ORM, because SQLObject is a little different from
other ORMs in this) you can forget you are storing the objects on a RDBM
(at least this is one way to use it, and yes, I agree 'select' is not a
good name ;). If it's too slow for your needs to do so, you can cut some
levels of abstraction and go back to the more SQL-ish constructs to gain
some speed (optimization, and as with any optimization, the code could
look uglier and less intuitive).

-- 
LUCA - Leandro Lucarella - Usando Debian GNU/Linux Sid - GNU Generation
------------------------------------------------------------------------
E-Mail / JID:     [EMAIL PROTECTED]
GPG Fingerprint:  D9E1 4545 0F4B 7928 E82C  375D 4B02 0FE0 B08B 4FB2 
GPG Key:          gpg --keyserver pks.lugmen.org.ar --recv-keys B08B4FB2
------------------------------------------------------------------------
Si ella es la flor, yo soy la espina
Si ella es quien florece, yo quien se marchita
Y estamos en eclipse total, y estamos en eclipse total
Completamente cruzados, completamente cruzados

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to