Error was: No module named sqlite3 - python

Settings for database are:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dataBase', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dataBase',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
and exception generates :
django.core.exceptions.ImproperlyConfigured: 'django.contrib.gis.db.backends.sqlite3' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named sqlite3.base
Following django documentations https://docs.djangoproject.com/en/1.7/ref/contrib/gis/tutorial/

change the conf. like this if you are using sqlite (by the this is just for reference made changes according to your need like dbname and etc.)
DATABASES = {
'default': {
'NAME': os.path.join(BASE_DIR, 'yourdbname.sqlite'),
'ENGINE': 'django.db.backends.sqlite3',
}
}
this will create db by name "yourdbname" where your apps settings.py resides plus you can refer to this link i guess it will help you

If you use sqlite3 to store the data, you should change the conf file,here is an example , just change the db file location:
DATABASES = {
'default': {
'ENGINE': 'sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': './db.db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dataBase',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}

Related

Try using 'django.db.backends.XXX', where XXX is one of:

I was setting Django up to use PostgresQL and for some reason, it won't connect it keep giving me this error:
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
here's the code for it in setting.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'DBNAME',
'USER': 'postgres',
'PASSWORD': 'DBPW',
'HOST': 'localhost'
}
}
I had the exact same code in a different project and it works perfectly fine!
you need to pip install psycopg2, looks like the Postgres adaptor is not installed
use this config instead of your config:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DBNAME',
'USER': 'postgres',
'PASSWORD': 'DBPW',
'HOST': 'localhost',
'PORT': '',
}
}
your ENGINE is not correct.

Running two databases on heroku with django

I have two databases that my Django application needs access. One is a shared database owned by a separate app with the Django app only having read access. The second is entirely owned by the Django app.
For local development I am ok but I'm not sure how to configure things so that Heroku uses the second database.
Currently I have the shared database promoted to DATABASE_URL and the secondary database is at HEROKU_POSTGRESQL_BLUE_URL.
In my settings I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Please ask any more questions if you need me to clarify.
Thanks!
In summary, my specific problem is: I don't know how to have Heroku use the HEROKU_POSTGRESQL_BLUE_URL as the "secondary" database.
---Edit----
At the bottom of settings.py:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
This is where the connection is made between my app's default database and Heroku's DATABASE_URL. I still haven't solved the issue but after some troubleshooting help in the comments, I believe the answer will be found in there.
Here is my working solution.
I have a local_settings.py file not tracked by version control. This contains the database settings for the developer's postgres instance.
local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Then in settings.py I try to import the local_settings.py file. If it doesn't exist, I use the django_heroku package to configure everything for Heroku. In my case, I need to pass the keyword argument databases=False since I want to do a more custom configuration for the databases.
settings.py (only the relevant parts)
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# These database settings are used for heroku deployments. They are overwritten with local_settings.py in development.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('SECONDARY_DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
try:
from local_settings import *
except ImportError as e:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals(), databases=False)
In order for this to work you would need to set the Heroku env variable SECONDARY_DATABASE_NAME to the url of your database.
To find out the URL of your secondary database run heroku config -a your_app_name. Copy the url and then set a new environment param in heroku by running
heroku config:set SECONDARY_DATABASE_URL=postgres://xxxxxxx -a your_app_Name
There are a lot of choices with how you would handle the ENV variables, I liked this because I can just set it in my staging and production environments and the same settings work for both.

Cannot connect MySQL database from django

I am trying to connect my MySql server from Django application , but whenever I am running the command python manage.py runserver it showing the error:
File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/connections.py", line 187, in __init__
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'user'#'localhost' (using password: NO)")
But my settings.py connection string is like :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_personnel', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'personnel', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
}
}
But I can easily connect the database from mysql shell :
mysql -u root -ppersonnel django_personnel
Please guide me. Thanks in advance.
It seems that your current user is not having permissions to connect to DB.
Re installing mysql will help
and also
'HOST': '',
'PORT': '',
Leave these two fields empty

Django testing MySQL permission

I am going through the Django tutorial and I've run into some trouble because I'm using MySQL as my database.
When I run
python manage.py test polls
I get
Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'USER123'#'%' to database 'test_USER123'")
Type 'yes' if you would like to try deleting the test database 'test_USER123', or 'no' to cancel:
This is my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'USER123', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'USER123',
'PASSWORD': 'PASSWORD',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
I was wondering if anyone would explain to me what's wrong and what's going on. Thank you!

Using sqlite3 within Google App Engine?

I am trying to deploy my Python + Django project to the Google App Engine. Right now it works fine on my local computer, but when I try running it as a project within the Google App Engine, I get the following error.
ImproperlyConfigured: 'django.db.backends.sqlite3' isn't an available database backend.
Try using django.db.backends.XXX, where XXX is one of:
'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sqlite3'
Error was: cannot import name utils
Here is the part of my settings.py file that specifies the sqlite3 database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mydb.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
Google App Engine requires that you use its own datastore, rather than sqlite or another database. There is a project that will allow you to use pretty much regular Django models on App Engine called django-nonrel. You can find more information about setting it up here: http://code.google.com/appengine/articles/django-nonrel.html

Categories

Resources