1

Otro problema que tengo es que trabajo con 2 base de datos como había dicho en la pregunta Paquete pyodb se conecta a SQLSERVER 2008 y no obtiene datos , y la base de datos db_app2 lo que es usuario, contraseña, servidor y el nombre de la base de datos lo pone el cliente desde el visual en la configuración que lo obtengo y guardo en un archivo .py

def save_db_settings_to_file(form):
path_to_store_settings = os.path.join(settings.BASE_DIR, 'SystemProyect')
for fname in os.listdir(path_to_store_settings):
    if fname=='conexion.cfg':
        full_path = os.path.join(path_to_store_settings, fname)
        remove(full_path)

database_id = 'db_app2'  # just something unique
newDatabase = {}
newDatabase["id"] = database_id
newDatabase['ENGINE'] = 'sql_server.pyodbc'
newDatabase['NAME'] = form.data['database']
newDatabase['USER'] = form.data['usuario_sql']
newDatabase['PASSWORD'] = form.data['password_sql']
newDatabase['HOST'] = form.data['servidor_sql']
newDatabase['PORT'] = ''
settings.DATABASES[database_id] = newDatabase

newDbString = """DATABASES={'%(id)s':{
'ENGINE': '%(ENGINE)s', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '%(NAME)s',                      # Or path to database file if using sqlite3.
'USER': '%(USER)s',                      # Not used with sqlite3.
'PASSWORD': '%(PASSWORD)s',                  # Not used with sqlite3.
'HOST': '%(HOST)s',                      # Set to empty string for localhost. Not used with sqlite3.
'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
'OPTIONS':"{'driver': 'SQL Server Native Client 10.0',}"
} }
"""% newDatabase
file_to_store_settings = os.path.join(path_to_store_settings, 'conexion' + ".py")
salvar=open(file_to_store_settings,'w')
salvar.write(newDbString)
salvar.close()

y en un fichero python llamado settings_manager.py tengo este codigo que le hago un import settings_manager en setitings.py

import os
path_to_store_settings =  os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'SystemProyect')
for fname in os.listdir(path_to_store_settings):
  if fname == 'conexion.py':
      full_path = os.path.join(path_to_store_settings, fname)
      with open(full_path) as fobj:
        archivo_inicio = fobj.read()
    exec(archivo_inicio)

así me guarda el archivo conexion.py

DATABASES = {
    'db_app2': {
        'ENGINE': 'sql_server.pyodbc',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'prueba',  # Or path to database file if using sqlite3.
        'USER': 'tony',  # Not used with sqlite3.
        'PASSWORD': '123456',  # Not used with sqlite3.
        'HOST': '10.16.41.2',  # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',  # Set to empty string for default. Not used with sqlite3.
        'OPTIONS': "{'driver': 'SQL Server Native Client 10.0',}"
    }
}

Mi problema es que no carga los datos para utilizarlo en algunos métodos que hago consultas a la base de datos a partir de los modelos hechos con inspecdb o con conections['db_app2'].cursor() y me devuelve el error que no encuentra la base de datos ´db_app2´, y cuando lo pongo normal en el setings.py me funciona

2
  • ¿Que error devuelve cuando intentas hacer una consulta en esta segunda base de datos? el 17 ago. 2018 a las 8:19
  • Que no encuentra la base de datos db_app2
    – Antonio
    el 18 ago. 2018 a las 3:28

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.