【问题标题】:Create a Django Database创建一个 Django 数据库
【发布时间】:2017-01-16 15:23:18
【问题描述】:

此代码可在其他人的本地计算机上运行 - 我们尚未在生产环境中运行它。但我的不工作。一位同事表示我需要创建一个数据库。在使用 mysql 之前,我使用的是 sqlite,它不需要这个。

当我运行 python manage.py runserver 这是我得到的:

XX-MacBook-Pro:xx xx$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x104ebb668>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
  fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
  self.check(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 426, in check
  include_deployment_checks=include_deployment_checks,
File "/Library/Python/2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
  new_errors = check(app_configs=app_configs)
File "/Library/Python/2.7/site-packages/django/core/checks/model_checks.py", line 28, in check_all_models
  errors.extend(model.check(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 1170, in check
errors.extend(cls._check_fields(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 1247, in _check_fields
  errors.extend(field.check(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 925, in check
  errors = super(AutoField, self).check(**kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 208, in check
  errors.extend(self._check_backend_specific_checks(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 317, in _check_backend_specific_checks
  return connections[db].validation.check_field(self, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 18, in check_field
  field_type = field.db_type(connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 625, in db_type
  return connection.data_types[self.get_internal_type()] % data
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
  return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 184, in data_types
  if self.features.supports_microsecond_precision:
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
  return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 359, in mysql_version
  with self.temporary_connection():
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
  return self.gen.next()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection
  cursor = self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 233, in cursor
  cursor = self.make_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
  self.ensure_connection()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  self.connect()
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 95, in __exit__
  six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  self.connect()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
  self.connection = self.get_new_connection(conn_params)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
  conn = Database.connect(**conn_params)
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
  return Connection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
  super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'testdb'")

我的设置文件有这个:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

...

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'testdb',
    'USER': ‘xx’,
    'PASSWORD': ‘xx’,
    'HOST': 'localhost'
  }
}

我已经测试了用户、密码并且知道它们可以工作。我的 MySQL 功能齐全。

如果我删除 DATABASES 'NAME' 我会得到:

django.db.utils.OperationalError: (1046, 'No database selected')

【问题讨论】:

    标签: python mysql django django-settings


    【解决方案1】:

    大概你已经通过进入 mysql shell 测试了用户名和密码。所以你可以再次做同样的事情,然后从那里做CREATE DATABASE testdb

    【讨论】:

    • 当我通过 python manage.py shell 进入 shell 并输入 CREATE DATABASE testdb 我得到:&gt;&gt;&gt; CREATE DATABASES testdb File "&lt;console&gt;", line 1 CREATE DATABASES testdb ^ SyntaxError: invalid syntax &gt;&gt;&gt;
    • 该命令应该输入到 SQL shell,而不是 Python shell。运行 python manage.py dbshell 而不是 python manage.py shell
    • 就是这样。感谢您的帮助,约翰·戈登!
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2018-01-18
    • 2010-09-06
    • 2023-03-16
    • 2014-04-15
    • 2021-06-07
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多