top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the proper wayto execute dynamic MySQL queries in Python

0 votes
701 views

What is 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.

posted May 29, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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.

answer May 29, 2013 by anonymous
Similar Questions
+1 vote

$ python -m pip install MySQL-python

Collecting MySQL-python
 Downloading MySQL-python-1.2.5.zip (108kB)
 100% | –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ| 112kB 260kB/s
 930 [main] python2.7 12948 child_info_fork::abort: address space needed by 'datetime.dll' (0x870000) is already occupied
 Error [Errno 11] Resource temporarily unavailable while executing command python setup.py egg_info
Exception:
Traceback (most recent call last):
 File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
 status = self.run(options, args)
 File "/usr/lib/python2.7/site-packages/pip/commands/install.py", line 324, in run
 requirement_set.prepare_files(finder)
 File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 380, in prepare_files
 ignore_dependencies=self.ignore_dependencies))
 File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 634, in _prepare_file
 abstract_dist.prep_for_dist()
 File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 129, in prep_for_dist
 self.req_to_install.run_egg_info()
 File "/usr/lib/python2.7/site-packages/pip/req/req_install.py", line 439, in run_egg_info
 command_desc='python setup.py egg_info')
 File "/usr/lib/python2.7/site-packages/pip/utils/__init__.py", line 667, in call_subprocess
 cwd=cwd, env=env)
 File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
 errread, errwrite)
 File "/usr/lib/python2.7/subprocess.py", line 917, in _execute_child
 self.pid = os.fork()
OSError: [Errno 11] Resource temporarily unavailable

Anyone hit similar issue?

+3 votes

I am using Python 2.7, MySQl 5.5, mysqldb(driver) and trying to connect to the mysql db using Python.

What I have tried

import MySQLdb
db = MySQLdb.connect(host="localhost",
                     user="root", # username
                      passwd="xxxxx", #  password
                      db="TEST") # name of the data base

Even tried 127.0.0.1 as local host

#ERROR
Traceback (most recent call last):
  File "C:/Python27/connect.py", line 6, in <module>
    db="prakash") # name of the data base
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
  OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

I am new to python, please help how to proceed?

+1 vote

Is there a way to make python script that connects to mySQL DB ask for a password on the:

conn = mdb.connect(host, user)
0 votes

I am having trouble matching Python data types with those of MySQL. MySQL has about 7 basic data types including Blobs, Binaries, etc. It also has a rich selection of geometry types. In addition, types like INT have 7 or 8 different options like Primary Key, zero fill, auto inc, etc. I can't seem to find anything in python to match these. I am trying to build a model.py for an existing database that was created with MySQL Workbench.

I do not wish to use anything other than MySQL because of the complexity of my storage needs (searchable text, PDF, Audio, Video and Photos). The text searches will often be word phrase searches through thousands of documents. I need the speed and the data type flexibility.

How do I build or modify a model.py to do this. I really don't want to write a model.py manually. That would really be a major pain. Workbench to the database an import to Django and edit is my choice. Unfortunately, the model.py produced has lost most of the functionality of the original database and I can't seem to figure out how to fix it.

+3 votes

I have a datafeed which is constantly sent to a MySql table. The table grows constantly as the data feeds in. I would like to write a python script which process the data in this table and output the processed data to another table in another MySql database in real-time.

Which are the python libraries which are suitable for this purpose? Are there any useful sample code or project on the web that I can use as reference?

...