string.join question

andy surany mongo57a at comcast.net
Wed Oct 2 23:28:48 EDT 2002


I'm accessing MySQL from Python. This is for an insert statement which looks
like this:


   values = [input1, input2]
# input values are derived from askstring, ex: input1 = aaa, input =bbb
   sqlcmd = sql.Insert_Cmd (tables, columns, values)
-----------------------------------------------------------------------
  def Insert_Cmd(self,tables,fields,values):

    insert_str = 'INSERT INTO %s ( %s ) VALUES ( %s )' %
(string.join(tables,','),string.join(fields,','),string.join(values,','))

This yields: INSERT INTO tablename (column1,column2) VALUES (aaa,bbb)

What I need is:
INSERT INTO tablename (column1,column2) VALUES ('aaa','bbb')

Regards,

Andy

-----Original Message-----
From: Michal Wallace <sabren at manifestation.com>
To: mongo57a at comcast.net <mongo57a at comcast.net>
Cc: python-list at python.org <python-list at python.org>
Date: Wednesday, October 02, 2002 9:54 PM
Subject: Re: string.join question


>On Thu, 3 Oct 2002 mongo57a at comcast.net wrote:
>
>> Thanks - it "kind of" works - but not quite. So now there is a part 2 of
the
>> question:
>>
>> value = 'aaa'
>> a = string.join(string.join(["'", value,"'"],''),',')
>>
>> This should yield (what I want.....): 'aaa',
>>
>> Instead it yields: ',a,a,a,'
>>
>> ??????????????????????? Looks crazy to me......
>
>No, it's right, because join takes a sequence. Your "aaa"
>string is a sequence of characters. :)
>
>What are you really trying to do? :)
>
>Cheers,
>
>- Michal   http://www.sabren.net/   sabren at manifestation.com
>------------------------------------------------------------
>Switch to Cornerhost!             http://www.cornerhost.com/
> Low Priced, Reliable Blog Hosting, With a Human Touch. :)
>------------------------------------------------------------
>





More information about the Python-list mailing list