django项目中创建数据库(Mac)

本文介绍了如何在Mac环境下,使用Django项目结合MySQL数据库进行操作。首先展示了在项目的settings.py中配置数据库信息,然后在虚拟环境中创建数据库。接着详细说明了如何在PyCharm中连接数据库并进行测试,确保连接成功。最后,通过迁移数据库文件并刷新,完成数据库的创建和迁移过程。

django项目中创建数据库

项目名/setting代码

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.mysql',#驱动
        'NAME': '数据库名',
        'USER': '用户',
        'PASSWORD': '密码',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

在虚拟空间里面创建数据库

#进入mysql目录文件
cd /usr/local/mysql/support-files 

#1. 启动mysql服务
sudo /usr/local/mysql/support-files/mysql.server start

#2. 停止mysql服务
sudo /usr/local/mysql/support-files/mysql.server stop


#mysql的进入和退出,mac-terimate
# 登陆前要确保mysql服务器打开
/usr/local/mysql/bin/mysql -u root -p
#再输入mysql密码(我的密码设置为root)

mysql> show databases;
+---------------------------------------+
| Database                              |
+---------------------------------------+
| information_schema                    |
| 2019_11_12_first_table                |
| Django_DB_01                          |
| flask_project_db                      |
| mysql                                 |
| performance_schema                    |
| python1903_fifth_day_developconfig_db |
| python1903_flask_day04                |
| python1903_flask_day06_develop        |
| python1903flask_01                    |
| sys                                   |
| tsjc                                  |
| vedio_web                             |
+---------------------------------------+
13 rows in set (0.01 sec)

mysql> create database Tsjc;
ERROR 1007 (HY000): Can't create database 'Tsjc'; database exists
mysql> create database Tsjc_website;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+---------------------------------------+
| Database                              |
+---------------------------------------+
| information_schema                    |
| 2019_11_12_first_table                |
| Django_DB_01                          |
| Tsjc_website                          |
| flask_project_db                      |
| mysql                                 |
| performance_schema                    |
| python1903_fifth_day_developconfig_db |
| python1903_flask_day04                |
| python1903_flask_day06_develop        |
| python1903flask_01                    |
| sys                                   |
| tsjc                                  |
| vedio_web                             |
+---------------------------------------+
14 rows in set (0.00 sec)

mysql> qiut;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'qiut' at line 1
mysql> quit
Bye

在pycharm项目中迁移数据库

1、点击右上角加号,选择databace/mysql,依据setting里面的设置填写相应信息

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XBbTfXH8-1586228101326)(/Users/manblue/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/Users/1102720253/QQ/Temp.db/90B98182-4464-477C-BAB3-835BF0131635.png)]

2、注意,在信息填好之后一定要注意先测试:点击test connection,如果是第一次还需下载驱动,测试成功后,点击Apply,最后点击OK,就成功啦。

3、迁移数据库文件

#在项目的终端下一次输入
#生成迁移文件
(django) manblue@appledeMacBook-Pro Tsjc % python manage.py makemigrations
System check identified some issues:

WARNINGS:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
No changes detected

#上传迁移文件
(django) manblue@appledeMacBook-Pro Tsjc % python manage.py migrate
System check identified some issues:

WARNINGS:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

4、点击刷新,大功告成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hCvACzp4-1586228101327)(/Users/manblue/Library/Containers/com.tencent.qq/Data/Library/Application Support/QQ/Users/1102720253/QQ/Temp.db/BADBDC8D-7E04-4044-B1FE-E7FA114FBE1B.png)]

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值