如何使用连接字符串从python连接到mysql数据库 [英] How to connect to mysql database from python using connection string
问题描述
我使用 python
和数据库作为 Mysql
,现在我想从 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
方法_1
import MySQLdb as mdb
conn = mdb.connect('ip_address', 'user_name', 'pass_word', 'database_name')
通过使用上面我可以成功连接到Mysql,但我想知道我们是否可以通过使用连接字符串并像下面提到的那样访问来做同样的事情
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
方法_2
connectString=Server=ip_address;Database=database_name;UID=user_name;PWD=pass_word
conn = mdb.connect(connectString)
但是我在上面使用时遇到错误,所以任何人都可以让我知道我们是否只能通过 method_1 访问 Mysql 数据库,或者有什么方法可以声明某个变量的访问凭据并使用正如我在 method_2
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
编辑代码:
实际上我正在尝试的内容如下
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
结果:
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)')
所以我正在尝试这种方式,因为我需要根据 example_file.ini
文件中的选择连接到两个数据库.有没有办法通过声明访问 .ini
文件中另一个变量的凭据来像 abobe 那样做.如果我从 .ini
文件中获取连接字符串,我期待在这里,它将这些字符串作为 string
.
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
.
推荐答案
不能,MySQLdb.connect
只支持前一个选项.
You can't, MySQLdb.connect
only supports the former option.
当然,您可以将连接字符串解析为其组成部分,并将其用作 .connect()
函数的一组关键字参数:
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)
然而,上面的拆分方法相当幼稚;您可能需要一个更复杂的方法来删除不支持的选项,转换某些参数值(想想 use_unicode
和 True
或 False
)并允许转义 ;
和 =
字符,它们是参数值的一部分.请参阅.connect()
文档a> 用于支持的关键字参数.
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.
这篇关于如何使用连接字符串从python连接到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!