a newbie question about gadfly

jm7potter at hotmail.com jm7potter at hotmail.com
Wed Jun 13 19:27:03 CEST 2001


On 13 Jun 2001 14:30:51 GMT, gbreed at cix.compulink.co.uk wrote:

>In article <vmoeit4n499fujuj5ine300ial3l94p81l at 4ax.com>, 
>jm7potter at hotmail.com () wrote:
>
>> On 12 Jun 2001 15:34:16 GMT, gbreed at cix.compulink.co.uk wrote:
>> 
>> >In article <hrbcit86g5gbmc63em9gc6tsji7nv78dqr at 4ax.com>, 
>> >jm7potter at hotmail.com () wrote:
>> >
>> >> Why? is namex not a direct replacement for "donna" ????
>> >
>> >Um, no, not the way you're doing it.  Try replacing that 
>magic 
>> >line with
>> >
>> >
>> >Or even
>> >
>> >cursor.execute("insert into students (name, grade) values 
>> >(%(namex)s, %(gradex)s)" % vars())
>> >
>> >(that's sure to wordwrap!)
>> >
>> >
>> >                 Graham
>> 
>> 
>> Thanks for the help Graham,
>> 
>> However, your code did not work either. The little program 
>chokes every 
>> time I try to
>> do anything that is not "hard-wired" into the script.
>
>Oops!  There is an error:
>
>cursor.execute(
>  "insert into students (name, grade) values ('%s', '%s')"
>  % (namex, gradex))
>
>might be better.  I forgot to quote the arguments, and at least 
>one of them is a string.
>

It Works?!?!! It is alive!!

Now, if you have the time and inclination --- could you explain why your first code
failed and the second worked? (I hate magic)

And further, could you explain to a newbie why *my* code failed?


By the way --- thank you so very much for your effort on my behalf. I really do
appreciate it much.





>> Oddly, the books that mention gadfly do not attempt anything 
>but 
>> hard-wired code.
>> Perhaps they know something?
>> 
>> Hammond & Robinson cover gadfly in their "Python: programming 
>on Win32" 
>>  (pp. 256
>> -259) but never attempt to get data from a user and send it to 
>the 
>> database.
>
>Oh, you have that?  I think the example right at the bottom of 
>p.257 should do what you want.  In your case
>
>insertstat = "insert into students (name, grade) values (?, ?)"
>cursor.execute(insertstat, (namex, gradex))
>

This does not work. I tried it before asking here on this ng.

In fact, your suggestion is the very code I tried. I am at a loss as to why it does
not work in Python. It shows that I am missing some big deal in my understanding of
the language. Some major flaw in my understanding.




>
>Just in case that doesn't work, I ran this code on the test 
>database as set up in the middle of p.257
>
>>>> bar = 'guiodos'
>>>> drinker = 'tim'
>>>> perweek = 35
>>>> cursor.execute("insert into Frequents(perweek, bar, drinker) 
>values (%s, '%s', '%s')"%(perweek, bar, drinker))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Yes, I see how this would work --- I did not even need to try it.

But, this is not what was used on page 257.

Only the very generous Mr. Graham showed me how to do it.

Perhaps you should do a book?



>>>> cursor.execute('select * from frequents')
>>>> print cursor.pp()
>PERWEEK | BAR      | DRINKER
>============================
>1       | lolas    | adam
>3       | cheers   | woody
>5       | cheers   | sam
>3       | cheers   | norm
>2       | joes     | wilt
>1       | joes     | norm
>6       | lolas    | lola
>2       | lolas    | norm
>3       | lolas    | woody
>0       | frankies | pierre
>1       | pans     | peter
>35      | guiodos  | tim
>
>
>>  For that matter, in "Learning Python" I see that Lutz & 
>Ascher do not 
>> even attempt
>> to get user input until page 200 when as an aside to exception 
>handling 
>> they tell us
>> about raw_input. Well, they do get user input via command line 
>> arguments, but that
>> can take one only so far.
>
>And raw_input can only get you so far as well, so the emphasis 
>is on GUIs for user input.
>

I learned C about 15 years ago. I learned to use the language with very simple i/o
--- and later explained to more sophisticated GUI interfaces.

Some of us old guys may need to do the same with Python.





>
>                Graham


Thank you very much --- your willingness to help out an old newbie is greatly
appreciated.




Regards, Joe




More information about the Python-list mailing list