[Tutor] Get variable values

Alan Gauld alan.gauld at btinternet.com
Thu Feb 1 23:55:04 CET 2007

> I have this code (following my signature) and i'm trying
> to get the value of "content" and insert it into a MySQL
> database.

>From where are you trying to get the value of content?
Its not clear from your code.

> def get_date(self, key):
>   ...

The fact that this has a self param,eter suggets this is
a method extracted from within a class? But without knowing
anything about that class its hard to know whats going on.

>      def get_content(self, key):
>         """Return the key containing the content."""

This doesn't return the key. It seems to return the content
corresponding to the key or an empty string. Is that what
you mean?

         for key in ("content", "tagline", "summary"):
             if self.has_key(key) and self.key_type(key) != self.NULL:
                 return self.get_as_string(key)

and if this works you will never do the insert.

So you only do the insert if you don't find a key, and
in that case you leave the value "summary" as the key
But that doesn't matter because you always execute exactly
the same INSERT statement - since its a hard coded string -
overwriting the previous one each time.

         db = MySQLdb.connect(host="localhost", user="planet",
passwd="secret", db="planet_geek")
         cursor = db.cursor()
         cursor.execute("INSERT INTO blog_posts (title) VALUES (key)")

         return ""

I think I'm confused, and I'm still not sure what you problem is.
We can't answer the question of why you don't get content
because thats done by the get_as_string() method which you
don't include in your listing.

Alan G. 

More information about the Tutor mailing list