简体   繁体   中英

Python MySQLdb get connection details

MySQLdb.connect()之后, MySQLdb.connect()从MySQLdb连接对象访问信息,例如主机,用户名和密码?

It doesn't seem to be way of retrieving the connection parameters. Or least one that could be recommended.

I've been looking at all the variables and methods of the object (in ipython you can invoke dict over the object that has the connection, eg conn. dict ) and the list given is something like:

{'_server_version': (5, 6),
 '_transactional': 8192L,
 'cursorclass': MySQLdb.cursors.Cursor,
 'encoders': {bool: <function MySQLdb.converters.Bool2Str>,
  instance: <function MySQLdb.converters.Instance2Str>,
  float: <function MySQLdb.converters.Float2Str>,
  int: <function MySQLdb.converters.Thing2Str>,
  list: <function MySQLdb.converters.quote_tuple>,
  long: <function MySQLdb.converters.Thing2Str>,
  dict: <function _mysql.escape_dict>,
  NoneType: <function MySQLdb.converters.None2NULL>,
  set: <function MySQLdb.converters.Set2Str>,
  str: <function MySQLdb.connections.string_literal>,
  tuple: <function MySQLdb.converters.quote_tuple>,
  object: <function MySQLdb.converters.Instance2Str>,
  unicode: <function MySQLdb.connections.unicode_literal>,
  datetime.timedelta: <function MySQLdb.times.DateTimeDelta2literal>,
  datetime.datetime: <function MySQLdb.times.DateTime2literal>,
  array.array: <function MySQLdb.converters.array2Str>},
 'messages': [],
 'string_decoder': <function MySQLdb.connections.string_decoder>,
 'unicode_literal': <function MySQLdb.connections.unicode_literal>}

By navigating through the code "connect" returns an instance of the class Connection that extends the existing _mysql.connection class. Here you can find functions like "get_host_info" that you could invoke directly on the connection obj but doesn't seem to provide that I think you are looking for.

A way of retrieving that data is creating a new class which contains the connection and the parameters given to it.

Hope that helps, Gemma

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM