Bhartendu Bharti
Bhartendu Bharti

Reputation: 71

how to set up config file to access mysql database parameter

hi im new to python cherrypy framework.

how to set up config file to use mysql database object.

myapp.conf contain

[global]

server.socket_host = "127.0.0.1"

server.socket_port = 8080

server.thread_pool = 10

server.thread_pool = 10

[Databases]

driver: "mysql"

host: "localhost"

username: "testusers"

password: "bharti"

dbtable: "employe"

[/path]

response.timeout: 6000

i want to use myapp conf file to set database parameter in my application code. how to access or use conf file...please help me out

Upvotes: 0

Views: 4780

Answers (1)

Andrew Kloos
Andrew Kloos

Reputation: 4598

try this...

server.conf:

[Database]
host: '192.168.0.1'
user: 'user'
passwd: 'passwd'
port: 3306
db: 'data'

and access the settings this way in your python file:

from MySQLdb.cursors import DictCursor
MySQLconnection = MySQLdb.connect(host=cherrypy.request.app.config['Database']['host'], 
                                  passwd=cherrypy.request.app.config['Database']['passwd'],
                                  db=cherrypy.request.app.config['Database']['db'],
                                  user=cherrypy.request.app.config['Database']['user'],
                                  port=cherrypy.request.app.config['Database']['port'], 
                                  cursorclass=DictCursor)
MySQLcursor = MySQLconnection.cursor()

Hope this helps!

Andrew

Upvotes: 1

Related Questions