【问题标题】:Error was: No module named sqlite3错误是:没有名为 sqlite3 的模块
【发布时间】:2015-03-19 21:52:16
【问题描述】:

数据库的设置是:

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.
    }
}

并产生异常:

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

遵循 django 文档https://docs.djangoproject.com/en/1.7/ref/contrib/gis/tutorial/

【问题讨论】:

  • 看起来你正在尝试做 GIS 的事情 - 除了 Postgres 之外,我不知道有任何数据库提供一些 GIS 功能。
  • 你安装配置好了SpatiaLite吗?

标签: python django sqlite


【解决方案1】:

更改配置。如果您使用的是 sqlite,就像这样(这仅供参考,根据您的需要进行更改,如 dbname 等)

DATABASES = {
    'default': {
       'NAME': os.path.join(BASE_DIR, 'yourdbname.sqlite'), 
       'ENGINE': 'django.db.backends.sqlite3',

  }
}

这将在您的应用程序 settings.py 所在的位置创建名称为“yourdbname”的数据库,并且您可以参考 this link 我想它会对您有所帮助

【讨论】:

【解决方案2】:

如果你使用sqlite3存储数据,你应该改变conf文件,这里是一个例子,只是改变db文件位置:

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.
    }
}

【讨论】:

  • db的位置应该是什么?
  • db文件的路径,如“/hone/user/malik/django/db.db”。只是一个绝对路径或当前路径'./db.db'就可以了。
  • 这里是另一个例外AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'
猜你喜欢
  • 2019-08-05
  • 2016-06-08
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 2017-12-26
相关资源
最近更新 更多