1. 程式人生 > >django 項目中mysql 的編碼,數據庫遷移問題

django 項目中mysql 的編碼,數據庫遷移問題

ins 管理 tcp lenovo initial mail acc com lte

以管理員的身份進入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

django 項目中mysql 的編碼,數據庫遷移問題