0

I have created a database(name=test) in mysql which stores the required configuration details in a table(name=config) to connect to other database as:

+-----+-------------+----------------+---------+-------------+------------+
| host  | user | password| db     | charset | cursorclass | drivername |
+-------+--------------+-------+----+---------+-------------+------------+
| ***** | **** | ******* | ****** | ***** | NULL        | pymysql    |
+------+-----+------+-----------+---------+-------------+------------+

Now using these values how can I establish a connection to other mysql database and these are running in local machines on same network.

I tried fetching the values to put in to configuration.py file as:(This is where I could not get any idea but tried my luck which doesn't work anyway )

def getdata():
     n = cursor.execute('select * from config')
     c = cursor.fetchall()
     lst=[]
     for i in c:
          lst.append([i[0],i[1],i[2],i[3],i[4],i[5]])
          return str(lst[0]])

I want to fetch the data from test db and use them as values in configuration file, any help is appreciated------------ Is there a way to approach to this??

configuration.py file:

import pymysql

connection=pymysql.connect(host='',
                           port= ,
                           user='',
                           password='',
                           db='',
                           charset='',
                           cursorclass=pymysql.cursors.DictCursor)
KcH
  • 3,302
  • 3
  • 21
  • 46
  • Looks like you're pretty much there, except I'm not sure why you turned the list into a string when you returned it from `getdata()`. – glibdud Aug 22 '19 at 12:08
  • when i tried to return the list i got an error like ,the return value must be a str,tuple but not list. Also how can i use them in config.py further? – KcH Aug 22 '19 at 12:13
  • You'll have to show the full traceback and the code where you're calling the function, because that's a strange error. – glibdud Aug 22 '19 at 12:14
  • is there any better way to get those values to use in configuration.py? – KcH Aug 22 '19 at 12:21
  • "Better" in what way? – glibdud Aug 22 '19 at 12:23
  • Maybe have a look at [this question](https://stackoverflow.com/questions/965694/whats-the-official-way-of-storing-settings-for-python-programs)? – glibdud Aug 22 '19 at 12:25
  • instead of getting all values in a list assigning each value from table to a variable and pass them to configuration.py – KcH Aug 22 '19 at 12:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198303/discussion-between-codenewbie-and-glibdud). – KcH Aug 22 '19 at 12:26
  • Don't have time to chat, I would just say that it's clear returning the config parameters as a list converted to a string isn't going to work. I recommend including your full attempt at returning it as a list along with the full traceback of the error you get. I think you're close to having what you're looking for. – glibdud Aug 22 '19 at 12:32
  • sure mate, I will try to figure it out and would see for any other recommendations – KcH Aug 22 '19 at 12:36

0 Answers0