标签:创建 int man bsp auth database 链接 ken end
新建django的配置
在项目下的settings.py配置文件下配置数据库
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘NAME‘: ‘student‘, ‘USER‘: ‘root‘, ‘PASSWORD‘: ‘123123‘, ‘HOST‘: ‘192.168.244.10‘, ‘PORT‘: ‘3306‘, }
__init__.py文件的配置
import pymysql pymysql.install_as_MySQLdb() #打开数据库链接 db = pymysql.connect("192.168.244.10","root","123123","student") #使用cursor()方法获取游标 cursor = db.cursor() cursor.execute("SELECT VERSION()") data = cursor.fetchone() print("Database version : %s" % data) db.close()
创建一个项目
创建项目后在settings.py文件中添加项目
INSTALLED_APPS = [ ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘, ‘myApp‘
在终端测试模板和数据库交互的情况
进入django的shell环境 E:\老男孩课程\tudent>python manage.py shell ########################################### >>> from myApp.models import Grades,Student >>> from django.utils import timezone >>> from datetime import * 通过模型类查看数据 >>> Grades.objects.all() 添加一条数据的本质 本质:创建一个模型类的对象 >>> grage1 = Grades() >>> grade1 = Grades() >>> grade1.gname = "python04" >>> grade1.gdate = datetime(year=2017,month=11,day=17) >>> grade1.ggirlnum = 3 >>> grage1.gboynum = 70 >>> grade1.gboynum = 70 >>> grade1.save()
标签:创建 int man bsp auth database 链接 ken end
原文地址:https://www.cnblogs.com/zhuhaofeng/p/9975704.html