[Tutor] MySQLdb INSERT with while or for loop
Decibels
decibelshelp@charter.net
Wed Jun 11 14:44:02 2003
Getting closer. I have changed it from a loop to just put the symbol string in.
But I want it to be a variable. Can't seem to get mysql to take a variable.
There must be a 'symbol' or something like @ or % that is want, otherwise
it will just put the item in as is.
Example:
value=(stock[0])
print "Value is %s" % value
cursor.execute("""
INSERT INTO stockinfo (symbol)
VALUES('@value')
""")
Just puts '@value' in the table column. Tried other ways, must be missing something.
When I print the value in the second line is says what stock[0] is, just can't get it
into the column.
On Wednesday 11 June 2003 12:47 pm, you wrote:
> On Wed, Jun 11, 2003 at 12:41:14PM -0500, Decibels wrote:
> > Ran into a slight sticking point with mysql. Created the tables fine, but
> > inserting information from a list is not sinking in.
> >
> > Does anyone have an idea how to use a FOR or WHILE loop to INSERT data
> > into a Table? If it needs more explaination, then pointing me to a doc
> > would be great. I am checking the MySQL site docs and nothing so far.
> >
> > The only examples I can find see to be explict inserts like below:
> >
> > cursor.execute ("""
> > INSERT INTO animal (name, category)
> > VALUES
> > ('snake', 'reptile'),
> > ('frog', 'amphibian'),
> > ('tuna', 'fish'),
> > ('racoon', 'mammal')
> > """)
> >
> >
> > But I am wanting to do something similar to: I added the ...... instead
> > of putting the whole table info
> >
> > cursor.execute("""
> > INSERT INTO stockinfo (symbol,name,.......)
> > VALUES
> > ( x=0
> > for x in results:
> > quotes9.cleanup(x)
> > )
> > """)
>
> The for loop would have to come outside of the execute statement as far as
> I can tell from your example.
>
> x=0
> for x in results:
> INSERT INTO stockinfo(symbol,name,........)
> VALUES
> (
> quotes9.cleanup(x)
> )
>
> Something like the above although I didn't spend much time or test it.
>
> --vicki