学API接口测试,致富植发(七)Sqlite3 数据库
程序员文章站
2024-03-22 09:36:52
...
一、在project的settings.py中修改配置,将数据库绝对路径换成相对路径
Django默认自带sqllite的数据库驱动,默认使用sqllite的数据库
二、在app的models.py 中创建类
from django.db import models
# Create your models here.
class Student(models.Model):
name= models.CharField(max_length=20)
age= models.IntegerField()
sex= models.CharField(max_length=1)
s_class= models.CharField(max_length=10)
class Meta:
# 设置数据库里的表名
db_table="student_info"
三、执行命令来生成数据表
1、
2、
四、查看数据表
五、往数据表添加数据
六、安装SQliteExpert,查看数据表
七、修改前面的view.py
1、导入部分
from django.shortcuts import render
from django.http.response import *
from django.contrib import auth
from django.contrib.auth.decorators import login_required
import json
from student.models import Student
2、查询学生信息部分
@login_required(login_url='/login')
def query_student(request):
"""
查询学生信息
:param request:
:return:
"""
# 判断请求方法
if request.method != 'GET':
return HttpResponseNotAllowed(['GET'])
name = request.GET.get('name')
if name:
students= Student.objects.filter(name= name)
data= [{"name": s.name,"age": s.age,"sex": s.sex,"classid": s.s_class} for s in students]
if data:
return JsonResponse({'result':0, 'msg': 'success', 'data': data})
else:
return JsonResponse({'result': 1, 'msg': 'fail', 'data': None})
else:
return JsonResponse({'result': 2, 'msg': 'parameter missing', 'data': None})
浏览器测试一下
3、添加学生信息部分
@login_required(login_url='/login')
def add_student(request):
"""
添加学生信息
:param request:
:return:
"""
if request.method != 'POST':
return HttpResponseNotAllowed(['POST'])
name = request.POST.get('name')
age = request.POST.get('age')
sex = request.POST.get('sex')
classid = request.POST.get('classid')
print('age type is', type(age))
print('age is', age)
student = {}
if not name:
return JsonResponse({'result': 1, 'msg': 'name missing'})
if age is not None and not( age.isdigit() and age[0] != '0'):
return JsonResponse({'result': 2, 'msg': 'age invalid'})
if sex is not None and sex not in ('男', '女'):
return JsonResponse({'result': 3, 'msg': 'sex invalid'})
student.update(name=name, age=int(age) if age is not None else 0,
sex=sex if sex is not None else '男',
classid=classid if classid is not None else 'T0000')
Student.objects.create(**student)
return JsonResponse({'result': 0, 'msg': 'success'})
代码测试一下(参照的是接口文档,不用管数据库)
import requests
if __name__ == '__main__':
s= requests.Session()
url= "http://127.0.0.1:8000/login"
data= {"username": "admin","password": "123456" }
ret= s.post(url= url,data= data)
print(ret.status_code)
print(ret.headers)
print(ret.text)
print("-----"*10)
url= "http://127.0.0.1:8000/student/add"
data= {"name": "mary","age": 26,"sex": "女", "classid": "W002"}
resp= s.post(url= url, data= data)
print(resp.status_code)
print(resp.text)
新增成功
七、查询数据库
先把db.sqlite3拷到当前目录下
# python第三方模块,pip安装
import pymysql
# python自带模块,无需安装
import sqlite3
if __name__ == '__main__':
# 连接数据库
conn= sqlite3.connect("./db.sqlite3")
cur= conn.cursor()
ret= cur.execute('''select * from student_info;''')
print(ret)
data= cur.fetchall()
print(data)
# 关闭数据库
conn.close()
执行结果
<sqlite3.Cursor object at 0x000002947CE85030>
[(1, 'tom', 22, '男', 'U001'), (2, 'mary', 26, '女', 'W002')]