Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46362

Re: MySQL dymanic query in Python

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 <fabiosantosart@gmail.com>
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&gt;': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '&gt;': 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 Fábio Santos <fabiosantosart@gmail.com>
To RAHUL RAJ <omrahulrajcse@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2345.1369821775.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

MySQL dymanic query in Python RAHUL RAJ <omrahulrajcse@gmail.com> - 2013-05-29 02:09 -0700
  Re: MySQL dymanic query in Python Fábio Santos <fabiosantosart@gmail.com> - 2013-05-29 11:02 +0100
    Re: MySQL dymanic query in Python RAHUL RAJ <omrahulrajcse@gmail.com> - 2013-05-30 05:22 -0700

csiph-web