当前位置:Gxlcms > 数据库问题 > django连接mysql数据库

django连接mysql数据库

时间:2021-07-01 10:21:17 帮助过:4人阅读

‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, #数据库引擎 ‘NAME‘: ‘django_db1‘, #数据库名称 ‘USER‘:‘root‘, #登录上述数据库的用户名 ‘PASSWORD‘:‘oldboy‘, #登录上述数据库的密码 ‘HOST‘:‘localhost‘, #登录上述数据库所在主机 ‘PORT‘:‘3306‘ #登录上述数据库的端口 } }

 

views.py

def index(request):
    cursor = connection.cursor()
    cursor.execute("insert into book(id,name,author) values(null,‘三国演义‘,‘罗贯中‘)") #通过execute执行sql语句
    rows = cursor.fetchall() #通过fetchall将所有结果取出来
    for row in rows:
        print(rows)
    return render(request,‘index.html‘)

运行项目,在浏览器输入127.0.0.1:8000,可看到数据库中增加了一条数据,并能在项目的终端看到输出结果

技术分享图片

 

django连接mysql数据库

标签:unit   setting   pre   engine   端口   lte   cut   tab   手动   

人气教程排行