Python+Django+mysql+unity(unity中C#用WWW向后端提交数据并存入数据库)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

unity中C#用WWW向django后端提交数据并存入mysql数据库


前言

主要解决发送多条数据,如果数据为空值暂时不接收,主要为判断了接收到数据的字符串长度


提示:以下是本篇文章正文内容,下面案例可供参考

一、Unity中C#(WWW)方法发送数据请求的数据请求

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SubmitKPbaoGao : MonoBehaviour
{
  
    public GameObject FinalScore;
    
    // Start is called before the first frame update
    public void OnClick()
    {
        StartCoroutine(TimeToSendMessage2());
       
    }
    IEnumerator TimeToSendMessage2()
    {
        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers.Add("Content_Type", "application/x-www-form-urlencoded");
        string data2 = "yx_score=" + "" + "&" + "cc_cz_time=" + "" + "&" + "cc_cz_score=" + FinalScore.GetComponent<Text>().text + "&" + "cc_gg_score=" + "" + "&" + "zz_cz_time=" + "" + "&"
                      + "zz_cz_score=" + "" + "&" + "zz_gg_score=" + "" + "&" + "kp_cz_time=" + "" + "&" + "kp_cz_score=" + "" + "&" + "kp_gg_score=" + "" + "&";

            ;

        byte[] bs2 = System.Text.UTF8Encoding.UTF8.GetBytes(data2);
        WWW www2 = new WWW("http://127.0.0.1/cccd", bs2, headers);
        yield return www2;
        {
            if (www2.error != null)
            {
                yield return null;
                Debug.Log("WWW2null");
            }
            else
            {
                Debug.Log("WWW2ok");
            }
        }
    }

  
}

发送多条数据,采用拼接字符串的形式,存在data里面

二、django后端接收view

class Cccd(View):
//配置自己的视图处理函数
	def get(self ,request):
		return render(request,'需要返回会的页面.html')
	def post(self,request):
		score1 = str(request.POST['yx_score'])
		score2 = str(request.POST['cc_cz_time'])
		........................................
		//上面C#代码拼接了几条数据就可以就收几条
		username = requset.COOKIES.get('username')
		user = User.objects.get(username = username)
		//从登录的cookies中获取用户名信息,把数据存入对应的用户
		if len(score1)>0:
		//判断字符串长度大于0,满足的话进行储存(及判断是否为空)
			user.score1 = score1
		user.save()
		if len(score2)>0:
			user.score2 = score1
		user.save()
		
		return HttpResponse('ok')
		

# 总结
<font color=#999AAA >提示:这里对文章进行总结:
主要为if len(score1)>0:判断接收到的值不为空,进行存储

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vtec-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值