以管理员的身份进入cmd
net start mysql
mysql -u root -p
没有密码直接回车,进入mysql可以创建数据库,
如退出mysql 执行 \q:命令
创建数据库 记得指定编码 create database orm_1128 character set utf8; orm_1128是数据库名字
修改数据库的字符集mysql>use mydb
mysql>alter database mydb character set utf8;
如果报错,
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
是因为在mysql5.6版本以后,就需要输入密码了,
设置新密码,123,下次再登录的时候就要输入密码123,
mysql> set password for root@localhost = password(‘123‘); Query OK, 0 rows affected, 1 warning (0.29 sec)
=====
查看数据库 ,可以看到已经创建的表
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bookmanage1 | | bookmanage2 | | db1 | | db2 | | gu_orm | | myorm | | mysql | | performance_schema | | s5orm | | school | | sys | +--------------------+ 12 rows in set (0.06 sec)
选择自己要修改的编码的 数据库,然后可以执行 \s; 命令 看到表的数据库的编码等信息,进行修改
mysql> use gu_orm; Database changed mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.18, for Win64 (x86_64) Connection id: 70 Current database: gu_orm Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.7.18 MySQL Community Server (GPL) Protocol version: 10 Connection: localhost via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: gbk-------------------------------------------------------------- Conn. characterset: gbk----------------------------------------------------------------------- TCP port: 3306 Uptime: 14 hours 39 min 56 sec Threads: 4 Questions: 860 Slow queries: 0 Opens: 365 Flush tables: 1 Open tables: 256 Queries per second avg: 0.016 -------------- mysql> set character_set_client=utf8;------修改编码 Query OK, 0 rows affected (0.05 sec)
创建表后如果出错---
File "C:\Python36\lib\site-packages\django\db\backends\mysql\base.py", line 30, in <module>
‘Did you install mysqlclient or MySQL-python?‘ % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named ‘MyS
QLdb‘.
Did you install mysqlclient or MySQL-python?
C:\Users\lenovo\PycharmProjects\gu_ORM_11month>
没有mysqldb(驱动)???
在python2之前的版本中是MySQLdb支持,到python3以后都用pymsql,
只需要调一下,在项目的下的__init__.py文件中 导入 import pymsql
写上 pymysql.install_as-MySQLdb() ,就行,
在创建一对多的表的时候,记得把主表,一 的表放在 多 的表前面,要不生成数据库时会报错
执行python manage.py makemigrations
python manage.py migrate
创建好数据库以后,可以在cmd里看下数据库,
use 数据库名字 ,进入数据库, \s 命令查看数据库信息
show tables; 查看表
mysql> use gu_ORM Database changed mysql> show table -> ; 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 ‘‘ at line 1 mysql> show tables; +----------------------------+ | Tables_in_gu_orm | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | django_admin_log | | django_content_type | | django_migrations | | django_session | | gu_orm_book | | gu_orm_publish | +----------------------------+ 12 rows in set (0.00 sec)
看到这里,就说明已经与数据库连接上了,与pycharm里的表一样
但每次都在cmd查数据不方便,就可以在pycharm里设置,
表就会出现,apply ,OK ,可以使用了
一对多的表,先对一 的表插入数据,然后在多的表才能插入数据,因为先对多的表插入数据时,有一个外键,需要数据,所以先对一 的表插入数据,
关于数据库的编码问题
show create table 数据库的表名; 查看 表的编码
alter table 数据库的表名 charset utf8; 修改表的编码
-------
重新迁移数据库时会发生的错误,
C:\Users\lenovo\PycharmProjects\gu_ORM_11month>python manage.py makemigrations No changes detected
记得在后面加上app名字
C:\Users\lenovo\PycharmProjects\gu_ORM_11month>python manage.py makemigrations gu_orm Migrations for ‘gu_orm‘: gu_orm\migrations\0001_initial.py - Create model Book - Create model Publish - Add field pu blish to book
在执行python manage.py migrate
C:\Users\lenovo\PycharmProjects\gu_ORM_11month>python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, gu_orm, 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 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 gu_orm.0001_initial... OK Applying sessions.0001_initial... OK