Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'insert': 0.05; 'subject:Python': 0.06; 'subject:query': 0.07; 'cc:addr:python- list': 0.11; 'stored': 0.12; "%s'": 0.16; 'attempted': 0.16; 'statement.': 0.16; 'type)': 0.16; 'wrote:': 0.18; 'email addr:gmail.com>': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'code:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'field,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "skip:' 10": 0.31; 'types.': 0.31; 'anyone': 0.31; 'table': 0.34; 'received:209.85': 0.35; 'received:google.com': 0.35; 'list': 0.37; 'list.': 0.37; 'received:209': 0.37; 'tell': 0.60; 'you.': 0.62; 'field': 0.63; 'to:addr:gmail.com': 0.65; 'here': 0.66; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=vT1S4aD+lC0KC1irKyVuembEAC6Al0JLQVXovJvTsPg=; b=nYW8FU5+u51nm8/QmILCvWA/mf/XQpSWdKUEwPYZ4/BLiP8CNAw4QHPrHHoY0GECMJ Kewg/+mwQkXCbkKl7bScHjbZX4niSoz8zCCpqHwV5ikRb0ykjpSp6gtcIfznTUgrqbgx DUeXbL1E3+ZHJhT4fQaBKoYAGIEyiZMvMDSCrC7BY8LTXtoDtujJ4NsEBrcZX2WYzKfT 7/Ljk8hHcIy2pchPJMXCmT507kdxDNPoF2cEl61tt6fZGt90hhdUAt3vv+3dzikCMQE1 YCqJ7nmSPPOza4ahMcHkQ/vPkSTgwIzRkdER34Jd4SfC4LvD/65bfotwZrT9i4WNTiT1 j+nw== MIME-Version: 1.0 X-Received: by 10.224.199.3 with SMTP id eq3mr2320866qab.18.1369821772194; Wed, 29 May 2013 03:02:52 -0700 (PDT) In-Reply-To: <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> References: <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> Date: Wed, 29 May 2013 11:02:51 +0100 Subject: Re: MySQL dymanic query in Python From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: RAHUL RAJ Content-Type: multipart/alternative; boundary=20cf300513e423441e04ddd87c65 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1369821775 news.xs4all.nl 15880 [2001:888:2000:d::a6]:33709 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46362 --20cf300513e423441e04ddd87c65 Content-Type: text/plain; charset=ISO-8859-1 On 29 May 2013 10:13, "RAHUL RAJ" wrote: > > Can anyone tell me the proper way in which I can execute dynamic MySQL queries in Python? > > I want to do dynamic queries for both CREATE and INSERT statement. > > Here is my attempted code: > > > sql="create table %s (%%s, %%s, %%s ... )" % (tablename,''.join(fields)+' '.join(types)) > cur.execute(sql) > > > where 'field' is the fieldnames stored in a list and 'types' are the fieldtypes stored in a list. You need to join the fields and the field types. Use zip(). Then join with commas. fields_and_types = ['%s %s' % (field, type) for field, type in zip(fields, types)] what_goes_between_the_parens = ', '.join(fields_and_types) sql = 'create table %s (%s)' % (tablename, what_goes_between_the_parens) See where that gets you. --20cf300513e423441e04ddd87c65 Content-Type: text/html; charset=ISO-8859-1


On 29 May 2013 10:13, "RAHUL RAJ" <omrahulrajcse@gmail.com> wrote:
>
> Can anyone tell me the proper way in which I can execute dynamic MySQL queries in Python?
>
> I want to do dynamic queries for both CREATE and INSERT statement.
>
> Here is my attempted code:
>
>
> sql="create table %s (%%s, %%s, %%s ... )" % (tablename,''.join(fields)+' '.join(types))
> cur.execute(sql)
>
>
> where 'field' is the fieldnames stored in a list and 'types' are the fieldtypes stored in a list.

You need to join the fields and the field types. Use zip().

Then join with commas.

fields_and_types = ['%s %s' % (field, type) for field, type in zip(fields, types)]

what_goes_between_the_parens = ', '.join(fields_and_types)

sql = 'create table %s (%s)' % (tablename, what_goes_between_the_parens)

See where that gets you.

--20cf300513e423441e04ddd87c65--