后端与数据库

一、导出数据表

远程服务器的SQL Server数据库导出数据表

步骤:在表所在的数据库上右击-->任务-->导出数据

image-20231113195548637 image-20231113195815030 image-20231113200201771 image-20231113200416024

我导出的是excel表格的文件格式

image-20231113200654181 image-20231113200758817 image-20231113200958250 image-20231113201047443 image-20231113201527214
二、传输至本地

将导出的数据表文件传输到本地电脑上(可以通过奶牛快传来实现),先在远程服务器上的浏览器搜奶牛快传,拖拽文件上传,然后分享生成密码,在本机通过密码直接下载文件

三、后端建表结构

python后端的model用来建立数据库的表结构(保证与远程服务器上的表结构完全一致)

image-20231113201834338

from django.db import models

# 进尺表
class FootAge(models.Model):
    # 字段名以及数据类型和是否为空
    id = models.IntegerField(primary_key=True)  # 主键
    FootDay = models.DateTimeField(null=True)
    downFootage = models.DecimalField(max_digits=20, decimal_places=6, null=True)
    SumDownFootage = models.DecimalField(max_digits=20, decimal_places=6, null=True)
    processage = models.IntegerField(null=True)
    UpperFootAge = models.DecimalField(max_digits=20, decimal_places=6, null=True)
    SumUpperFootage = models.DecimalField(max_digits=20, decimal_places=6, null=True)
    DaoShu = models.IntegerField(null=True)
    EditDownFootage = models.DecimalField(max_digits=20, decimal_places=6, null=True)
    EditUpperFootage = models.DecimalField(max_digits=20, decimal_places=6, null=True)
    Memo1 = models.CharField(max_length=200, null=True)
    Memo2 = models.CharField(max_length=200, null=True)

    class Meta:
        db_table = "c_footage"  # 这是数据库中的表的名字
四、迁移

通过两条迁移命令

//数据库迁移
python manage.py makemigrations

python manage.py migrate

将数据库的表结构迁移到数据库中

五、导入数据

数据库里导入远程传输来的数据库文件

在已建好表结构上的表上右击-->导入向导

image-20231114103118054 image-20231114103232467 image-20231114103634993 image-20231114103713937 image-20231114104043165 image-20231114104423985 image-20231114104323367 image-20231114104514079 image-20231114104643072

至此,在数据库对应的表中已经有数据了,若没有,在表上右击刷新即可

image-20231114104819050
六、后端查询字段及数据
def fun_get_supportmoveinfo_data(request):
    try:
        queryset = SupportMoveInfo.objects.all()  # 获取所有支架移动信息数据
        support_data_list = []

        for item in queryset:  # 遍历
            support_data = {
                "id": item.id,  # 后面是数据库中表的字段名,前面相当于在后端文件中调用它所使用的字段名
                "support_bh": item.SupportBh,
                "generate_time": item.GenerateTime,
            }
            support_data_list.append(support_data)  # 要想查询所有的字段,需要遍历,并用列表的形式承接它

        return JsonResponse({"support_data_list": support_data_list})  # 如果成功的话返回字段名和相应的值
    except SupportMoveInfo.DoesNotExist:  # 如果不成功则返回错误 
        return JsonResponse({"error": "No SupportMoveInfo data available."})

上述代码,查询了表中所有数据的前三个字段,正常运行后应该是下面的查询结果:

image-20231114110244630
posted @ 2023-11-14 11:07  燕妮wyn  阅读(57)  评论(0)    收藏  举报