Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #46361 > unrolled thread
| Started by | RAHUL RAJ <omrahulrajcse@gmail.com> |
|---|---|
| First post | 2013-05-29 02:09 -0700 |
| Last post | 2013-05-30 05:22 -0700 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | RAHUL RAJ <omrahulrajcse@gmail.com> |
|---|---|
| Date | 2013-05-29 02:09 -0700 |
| Subject | MySQL dymanic query in Python |
| Message-ID | <2fd0cae6-6cfe-4163-964e-2aa863e65f5e@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-05-29 11:02 +0100 |
| Message-ID | <mailman.2345.1369821775.3114.python-list@python.org> |
| In reply to | #46361 |
[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.
[toc] | [prev] | [next] | [standalone]
| From | RAHUL RAJ <omrahulrajcse@gmail.com> |
|---|---|
| Date | 2013-05-30 05:22 -0700 |
| Message-ID | <9f07fb7a-4d37-47cb-be22-67245dd73d1f@googlegroups.com> |
| In reply to | #46362 |
On Wednesday, May 29, 2013 3:32:51 PM UTC+5:30, Fábio Santos wrote: > On 29 May 2013 10:13, "RAHUL RAJ" <omrahu...@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. Fantastic! It worked, Thanks :)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web