Answer the question
In order to leave comments, you need to log in
How to connect to the database using *.ini configuration files?
Help, please, to understand.
I have a method for getting data from a db and it has a connection string:
connection_string = 'mysql://test:[email protected]/test_db'
e = create_engine(connection_string, echo=True)
How can I change the method to get the connection string from the configuration file (development.ini or some custom db.ini from the root folder)
The official documentation has an example:
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from .models import DBSession, Base
def main(global_config, **settings):
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
config = Configurator(settings=settings,
root_factory='tutorial.models.Root')
config.include('pyramid_chameleon')
config.add_route('wiki_view', '/')
config.add_route('wikipage_add', '/add')
config.add_route('wikipage_view', '/{uid}')
config.add_route('wikipage_edit', '/{uid}/edit')
config.add_static_view('deform_static', 'deform:static/')
config.scan('.views')
return config.make_wsgi_app()
def main(global_config, **settings):
engine = engine_from_config(settings, 'sqlalchemy.')
[app:main]
use = egg:tutorial
pyramid.reload_templates = true
pyramid.includes =
pyramid_debugtoolbar
pyramid_tm
sqlalchemy.url = sqlite:///%(here)s/sqltutorial.sqlite
def save(request):
data = request.json_body
user_hash = sha1(json.dumps(data).encode('utf-8')).hexdigest()
user = json.dumps(data.get("user"))
settings = data.get("settings")
s = orm.Session()
try:
connection_string = 'mysql://user_db:[email protected]/user_db'
e = create_engine(connection_string, echo=True)
meta = MetaData(bind=e, reflect=True)
...
Answer the question
In order to leave comments, you need to log in
See here:
https://stackoverflow.com/questions/31764020/worki...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question