Home Database Mysql Tutorial Python+Django+SAE系列教程12-----配置MySQL数据库_MySQL

Python+Django+SAE系列教程12-----配置MySQL数据库_MySQL

Jun 01, 2016 pm 01:15 PM
password database administrator

pythonDjango

因为SAE上支持的是Mysql,首先我们要在本地配置一个Mysql的环境 ,我在网上找到MySQL-python-1.2.4b4.win32-py2.7.exe,并双击 安装


选择典型安装


安装结束后,会弹出配置数据库的界面 :


然后输数据管理员密码:


最后是运行服务。


这个过程并不复杂,安装完成Mysql以后,系统会启动数据库服务,由于Mysql是用命令行控制的,想我这样懒的 人还是需要借助一个可视化 工具来管理,我选择MySQL-Front。

在网上找到MySQL_Front_Setup.1765185107.exe,双击安装就可以了,安装完打开软件:


这时我们会看到已经有的几个数据库:


我要开发程序首先,我们要建立一个数据库(右键 新建数据库),起名字为Mytestdb:


OK,这样我们就建立了一个空的数据库了,我先不着急在这里建表和表的关系。我们打算使用Django的模型的方式来建立:

首先我们修改一下

 setting.py,好让Django认识我们新建立的数据库:

ADMINS = (	('hemeng80', 'hemeng80@126.com'),)MANAGERS = ADMINSfrom os import environdebug = not environ.get("APP_NAME", "") if debug:	#LOCAL 	db_name = "MyTestDB"	name = "root"	pwd = "123456"	host = "127.0.0.1"	port = "3306"else: 	#SAE 	import sae.const	db_name = sae.const.MYSQL_DB	 	name = sae.const.MYSQL_USER	 	pwd = sae.const.MYSQL_PASS	 	host = sae.const.MYSQL_HOST		port = sae.const.MYSQL_PORT	 	host_s = sae.const.MYSQL_HOST_S DATABASES = {	'default': {		'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.		'NAME': db_name,					# Or path to database file if using sqlite3.		'USER': name,					# Not used with sqlite3.		'PASSWORD': pwd,				# Not used with sqlite3.		'HOST': host,					# Set to empty string for localhost. Not used with sqlite3.		'PORT': port,					# Set to empty string for default. Not used with sqlite3.	}}
Copy after login
上面的内容不需要多解释,你就会能明白,一种是在sae环境下的链接,一种是在本地的链接。

这时我们需要使用Django,生成一个模型,在命令行模式输入:

python manage.py startapp person

这个命令并没有输出什么,它只在 mysite 的目录里创建了一个 books 目录。 让我们来看看这个目录的内容:
person/
    __init__.py
    models.py
    tests.py
    views.py


这个目录包含了这个app的模型和视图。
使用你最喜欢的文本编辑器查看一下 models.py 和 views.py 文件的内容。 它们都是空的,除了 models.py 里有一个 import。这就是你Django app的基础。

目前我们的目录结构是这样的:


接下来我们来

编辑 person的models.py,来定义我们的数据模型:

models.py:

from django.db import models# Create your models here.class ClassRoom(models.Model):	name = models.CharField(max_length=30)	tutor = models.CharField(max_length=30)class Student(models.Model):	name = models.CharField(max_length=30)	sex = models.CharField(max_length=5)	age = models.IntegerField()	state_province = models.CharField(max_length=30)	qq = models.IntegerField()	classroom = models.ForeignKey(ClassRoom)
Copy after login
上面的数据模型比较简单,你一看就能明白,需要注意外键关系是这样建立的:
classroom = models.ForeignKey(ClassRoom)
Copy after login
为了让django认识我们新添加的App,还需要在setting.py中修改一些内容:
INSTALLED_APPS = (	'django.contrib.auth',	'django.contrib.contenttypes',	'django.contrib.sessions',	'django.contrib.sites',	'django.contrib.messages',	'django.contrib.staticfiles',	'person',	# Uncomment the next line to enable the admin:	# 'django.contrib.admin',	# Uncomment the next line to enable admin documentation:	# 'django.contrib.admindocs',)
Copy after login
这时,我们可以进入命令行程序,来检查一下我们的app是否正确:

python manage.py validate

如果没有问题的话,我们就可以使用这个模型来建立数据库中的表和表的关系了 ,命令行进入我们之前建立好的路径,输入:

python manage.py sqlall person


这样自动生成了,数据模型的建立表的SQl,语句,我们在mysql-front里面执行就可以自动创建数据库中的表了:


如果我们复制粘贴的话,这里面有一点问题,其中语句长的换行了,会执行不通过的,我们修改一下就行了:


在打开数据库,刷新看看,是不是我们表都自动创建了?


我们注意到Django自动创建了一个自增的主键!

既然我们在sae中开发,那么下面我就来看看如何在sae中创建一个Mysql的数据库,添加表的,找到sae的Mysql服务,点击初始化:


然后管理Mysql:


在这个里面执行以下我们刚才复制的sql语句创建表 :


这样我们就在本地和sae中创建了相同的表,并添加了响应的app,下一步就是如何对这个表进行最基本的操作了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? Apr 11, 2024 am 09:10 AM

When using Win11 system, sometimes you will encounter a prompt that requires you to enter the administrator username and password. This article will discuss how to deal with this situation. Method 1: 1. Click [Windows Logo], then press [Shift+Restart] to enter safe mode; or enter safe mode this way: click the Start menu and select Settings. Select "Update and Security"; select "Restart Now" in "Recovery"; after restarting and entering the options, select - Troubleshoot - Advanced Options - Startup Settings -&mdash

How to set router WiFi password using mobile phone (using mobile phone as tool) How to set router WiFi password using mobile phone (using mobile phone as tool) Apr 24, 2024 pm 06:04 PM

Wireless networks have become an indispensable part of people's lives in today's digital world. Protecting the security of personal wireless networks is particularly important, however. Setting a strong password is key to ensuring that your WiFi network cannot be hacked by others. To ensure your network security, this article will introduce in detail how to use your mobile phone to change the router WiFi password. 1. Open the router management page - Open the router management page in the mobile browser and enter the router's default IP address. 2. Enter the administrator username and password - To gain access, enter the correct administrator username and password in the login page. 3. Navigate to the wireless settings page - find and click to enter the wireless settings page, in the router management page. 4. Find the current Wi

Tutorial on changing wifi password on mobile phone (simple operation) Tutorial on changing wifi password on mobile phone (simple operation) Apr 26, 2024 pm 06:25 PM

Wireless networks have become an indispensable part of our lives with the rapid development of the Internet. In order to protect personal information and network security, it is very important to change your wifi password regularly, however. To help you better protect your home network security, this article will introduce you to a detailed tutorial on how to use your mobile phone to change your WiFi password. 1. Understand the importance of WiFi passwords. WiFi passwords are the first line of defense to protect personal information and network security. In the Internet age, understanding its importance can better understand why passwords need to be changed regularly. 2. Confirm that the phone is connected to wifi. First, make sure that the phone is connected to the wifi network whose password you want to change before changing the wifi password. 3. Open the phone’s settings menu and enter the phone’s settings menu.

What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements? What should I do if my Win10 password does not meet the password policy requirements? What to do if my computer password does not meet the policy requirements? Jun 25, 2024 pm 04:59 PM

In the Windows 10 system, the password policy is a set of security rules to ensure that the passwords set by users meet certain strength and complexity requirements. If the system prompts that your password does not meet the password policy requirements, it usually means that your password does not meet the requirements set by Microsoft. standards for complexity, length, or character types, so how can this be avoided? Users can directly find the password policy under the local computer policy to perform operations. Let’s take a look below. Solutions that do not comply with password policy specifications: Change the password length: According to the password policy requirements, we can try to increase the length of the password, such as changing the original 6-digit password to 8-digit or longer. Add special characters: Password policies often require special characters such as @, #, $, etc. I

How to easily get the WiFi password of a connected mobile phone (quickly find the WiFi password saved in the mobile phone) How to easily get the WiFi password of a connected mobile phone (quickly find the WiFi password saved in the mobile phone) May 08, 2024 pm 01:10 PM

Our mobile phones have become an integral part of our lives in modern society. Wireless network connections have also become an indispensable tool in our daily lives. However, sometimes we face such a situation: we want to connect to other devices but are unable to do so, we connect to WiFi but forget the password. How to easily get the WiFi password of a connected mobile phone? Find the saved WiFi password on the phone 1. Find the "WiFi" option in the settings, find and click it, and enter the phone's settings interface "WiFi" to enter the WiFi settings page, option. 2. Open the connected WiFi network details, find the name of the connected WiFi network, click to enter the detailed information page of the network, in the WiFi settings page. 3.

What is the name of the software that can unlock all wifi passwords (recommended mobile software to get connected wifi passwords with one click) What is the name of the software that can unlock all wifi passwords (recommended mobile software to get connected wifi passwords with one click) Apr 02, 2024 pm 06:46 PM

Wireless networks have become an integral part of people's lives in modern society. Or need to view saved passwords in WiFi list, sometimes we forget WiFi password, however. Mobile software came into being to solve this problem. To make it easy for you to master this magical tool, this article will introduce a software that can help you quickly unlock all WiFi passwords. 1. Powerful WiFi password unlocking tool - explore the wonderful world of WiFi password unlocking 2. Simple and easy-to-use interface design - convenient and fast operation methods 3. Supports multiple devices - meets the needs of different devices 4. Automatically Update - always keep the latest WiFi password 5. High-speed WiFi connection - easily cope with the simultaneous connection needs of multiple devices

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

See all articles