pycharm connects to mysql database

1. Set up pycharm

  The default font of pycharm is very small 12, modify it to 18, search for font in the setting, and set the font size.

  Set the shortcut key of pycharm to visual studio

  

 

  Configure django environment settings.py

  

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

 

zh-hans is a Chinese voice package only used by django2, the time zone uses 'Asia/Shanghai', the world time USE_TZ uses No False 

2. Create a mysql database

 


3. Set up mysql configuration

  

 

  Use the VS shortcut ctrl + alt + f to format the code, so that you don't have to manually adjust spaces or two empty lines.

  

 

  

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mxonline',
        'USER':'root',
        'PASSWORD': 'root',
    }
}

 

 name is the database, user is the user name, both host and por can be left blank, which is the default. The left key name must be in all uppercase. I wrote USER as User once before, and the error given by django said that the database could not be connected (password required) or something, and I couldn't find anything wrong. Later, when I accidentally saw the name of USER, I found out that the problem of capitalization .

 

 4. Run Tools > Run manage.py Task error, saying that the management dependency package mysqlclient is missing, just use pip to install it

pip install mysqlclient

  Note: When you make an error, first look at the prompts given by your own pycharm, because the environment versions in the tutorial are different, you don't need to follow the installation steps in the tutorial, this is how you learn anything.

  

  

 

 5. Migrate default data to mysql database

makemigrations
migrate

  First makemigrations generate the data that needs to be migrated, form a file, and then execute migrate to store the data that needs to be migrated in the document into mysql, create it when it needs to be created, and update it when it needs to be updated, fully automatic

  

  

  

  In this way, the django connection to the mysql database has been completed.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126628&siteId=291194637