Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to mysql database from python using connection string

Tags:

python

mysql

Hi i am using python and database as Mysql, Now i want to connect to Mysql database from python and i wrote the below code

Method_1

import MySQLdb as mdb


conn = mdb.connect('ip_address', 'user_name', 'pass_word', 'database_name') 

By using above i can connect to Mysql succesfully, but i want to know whether we can do the same by using a connection string and accessing like i mentioned below

Method_2

connectString=Server=ip_address;Database=database_name;UID=user_name;PWD=pass_word
conn = mdb.connect(connectString) 

But i am getting an error by using above, so can anyone let me know whether we can access Mysql database only by method_1 or is there any way to declare the access credentials to some variable and using that variable to connect as i mentioned in method_2

Edited Code:

Actually what i am trying is given below

example_file.ini

[for_primary]

connectString=host="192.168.xx.xxx",user="username_1",passwd="password_1",db="database_1"

[for_secondary]

connectString=host="192.168.xx.xxx",user="username_2",passwd="password_2",db="database_2"

file.py:

import ConfigParser
import MySQLdb as mdb

configFeed = ConfigParser.ConfigParser()
configFeed.read('path to file/example_file.ini')
connectString = configFeed.get('for_primary', 'connectString')
conn = mdb.connect(connectString)
print conn

Result:

 File "/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2005, 'Unknown MySQL server host \'host="192.168.xx.xxx",user="username_1", passwd="password_1",db="database_1"\' (0)')

So i am trying in this way because i need to connect to two databases depending upon selection in example_file.ini file. Is there any way to do like abobe by declaring to access credentials to another variable in .ini file. i am expecting is here if i get connection string from .ini file it taking those as string.

like image 610
Shiva Krishna Bavandla Avatar asked Aug 22 '12 12:08

Shiva Krishna Bavandla


1 Answers

You can't, MySQLdb.connect only supports the former option.

You can, of course, parse the connection string into it's constituents and use that as a set of keyword parameters for the .connect() function:

connectParams = dict(entry.split('=') for entry in connectString.split(';'))
mdb.connect(**connectParams)

The above splitting method is rather naive however; you probably would need a more sophisticated method that would remove unsupported options, convert certain parameter values (think use_unicode and True or False) and allow for escaping of ; and = characters where they are part of a parameter value. Refer to the .connect() documentation for supported keyword arguments.

like image 120
Martijn Pieters Avatar answered Oct 22 '22 10:10

Martijn Pieters