So I am building a Django site that is connecting to a view legacy DBs. Because of some of the limitations of the legacy MySQL dbs I need to access a view. From what I can tell the easiest way is to insert sql directly. (inspectdb doesn't work for the views).
Here is the code I have so far:
from django.db import connection
cursor = connection.cursor()
sqlstring = 'SELECT * FROM Site_Call_Counts'
cursor.execute(sqlstring)
result = cursor.fetchall()
This code works when I enter it into the manage.py shell
but breaks when I try and run it complete. Here is the traceback.
/usr/bin/python2.7 /root/Desktop/IPNV/django-test/src/djangotest/viewTest.py
Traceback (most recent call last):
File "/root/Desktop/IPNV/django-test/src/djangotest/viewTest.py", line 3, in <module>
cursor = connection.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 208, in __getitem__
self.ensure_defaults(alias)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 176, in ensure_defaults
conn = self.databases[alias]
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 156, in databases
self._databases = settings.DATABASES
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured.
So really I have 2 Questions. First, is this the easiest way to accomplish what I need, and 2nd, if it is how to I fix this error. I have looked at the docs and the posts here and here. The docs are django docs
This may be obvious but I am newer to coding so be gentle lol.
EDIT*** Here is my db definition in settings.py (same directory)
},
'default': {
'NAME': 'app2',
'ENGINE': 'django.db.backends.mysql',
'USER': 'admin',
'PASSWORD': 'password',
'HOST': '10.1.1.1',
'PORT': '3306',
},