2013-08-16 1 views
0

안녕하세요, 저는 새로운 Python cherrypy 프레임 워크를 처음 접했습니다.mysql 데이터베이스 매개 변수에 액세스하기 위해 설정 파일을 설정하는 방법

mysql 데이터베이스 개체를 사용하도록 설정 파일을 설정하는 방법.

myapp.conf 포함

[글로벌]

server.socket_host = "127.0.0.1"

server.socket_port = 8080

server.thread_pool = 10

server.thread_pool = 10

[데이터베이스]

드라이버 : "MySQL은"

호스트 : "localhost"를

이름 : "testusers"

비밀번호 : "바르 티"

DBTABLE "직원당"

[/경로]

response.timeout : 6000

내 응용 프로그램 코드에서 데이터베이스 매개 변수를 설정하기 위해 myapp conf 파일을 사용하고 싶습니다. 액세스 또는 사용의 conf 파일이 ... ...

답변

1

이 시도

server.conf 나 좀 도와주십시오 방법 :

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

을하고 파이썬 파일의 설정이 방법에 액세스 :

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() 

희망이 있습니다.

앤드류

+0

무엇이 dictcurosor입니까? cursorclass = dictcursor –

+0

적절한 코드로 코드를 업데이트했습니다. DictCursor를 사용하면 열 이름을 사용하여 데이터베이스에서 검색된 요소에 액세스 할 수 있습니다. 예를 들어, DatabaseResults [0] [ 'ColumnName']. 자세한 정보는 http://www.gadzmo.com/python/using-pythons-dictcursor-in-mysql-to-return-a-dict-with-keys/입니다. –

관련 문제