值始终返回0,它应该返回插入的行数 [英] Value always return 0 where it should return number of row inserted

查看:106
本文介绍了值始终返回0,它应该返回插入的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,一个是信息,另一个是员工

资讯

员工

该TWF 员工是用来获得 ID 的<强>资讯,它假设回报1,但它的返回值0。如果 ID 的信息 5 TWF 应该是5了。

首先,我使用 A 重新present选定行的数量,然后添加的参数 A addWorkForce 。在面包始终显示0。

  A = addInformation(姓名,天气,DATE2,状态,first1 [1],last1 [1]);
  Toast.makeText(getApplicationContext(),一个+,Toast.LENGTH_LONG).show();
  addWorkForce(SUB,NOP,NOH,一);

addInformation功能

 众长addInformation(最终字符串名称,最后字符串的天气,最终字符串DATE2,最后弦乐状态,最后弦乐timeIn,最后弦乐超时)
    {
        类ADDINFO扩展的AsyncTask&LT;弦乐,太虚,字符串&GT; {
            ProgressDialog负荷;            @覆盖
            在preExecute保护无效(){
                super.on preExecute();
                装载= ProgressDialog.show(WorkDetailsTable.this,请等待,空,真,真);
            }            @覆盖
            保护无效onPostExecute(String s)将{
                super.onPostExecute(多个);
                loading.dismiss();
                Toast.makeText(getApplicationContext()中,s,Toast.LENGTH_LONG).show();
            }            @覆盖
            保护字符串doInBackground(字符串... PARAMS){                HashMap的&LT;字符串,字符串&GT;数据=新的HashMap&LT;字符串,字符串&GT;();
                data.put(Config.KEY_USER_NAME,名);
                data.put(Config.KEY_WEATHER,天气);
                data.put(Config.KEY_DATE,DATE2);
                data.put(Config.KEY_STATUS,地位);
                data.put(Config.KEY_TIMEIN,timeIn);
                data.put(Config.KEY_TIMEOUT,超时);
                RequestHandler RH =新RequestHandler();
                字符串结果= rh.sendPostRequest(Config.ADD_INFORMATION,数据);
                返回结果;
            }
        }         ADDINFO RU =新ADDINFO();
         ru.execute(姓名,天气,DATE2,地位,timeIn,超时);        返回0;    }

有人可以帮我找出这个问题?

addInformation.php

 &LT; PHP    如果($ _ SERVER ['REQUEST_METHOD'] =='POST'){        //获取值
        $名称= $ _ POST ['名'];
        $天气= $ _ POST ['天气'];
        $日期= $ _ POST ['日期'];
                $状态= $ _ POST ['地位'];
                $ timeIn = $ _ POST ['timeIn'];
                $ TIMEOUT = $ _ POST ['超时'];        //创建一个SQL查询
        $的SQL =INSERT INTO信息(姓名,天气,日期,状态,time_in,TIME_OUT)VALUES('$名称','$天气','$日期,$状态,$ timeIn','$超时');        //导入我们的数据库连接的脚本
        require_once('dbConnect.php');        //执行查询数据库
        如果(mysqli_query($ CON,$ sql中)){
            回声信息添加成功';
        }其他{
            echo'不能添加信息;
        }        //关闭数据库
        mysqli_close($ CON);
    }
?&GT;

这是我使用的code拿到插入源码行和它的作品

  A = ts.insertTimeSheet(姓名,天气,DATE2,状态,first1 [1],last1 [1]);

的TimeSheet

 众长insertTimeSheet(字符串名称,字符串天气,日期字符串,字符串状态,字符串TimeIn,字符串超时){
        数据库= dbHelper.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(MyDatabaseHelper.Name,名);
        values​​.put(MyDatabaseHelper.Weather,天气);
        values​​.put(MyDatabaseHelper.Date,日期);
        values​​.put(MyDatabaseHelper.Status,地位);
        values​​.put(MyDatabaseHelper.TimeIn_Info,TimeIn);
        values​​.put(MyDatabaseHelper.TimeOut_Info,超时);
        返回database.insert(MyDatabaseHelper.TABLE_INFO,空,价值); //如果信息中的i​​d是5,TWF显示5太


解决方案

根据您的code以添加信息:

  A = addInformation(姓名,天气,DATE2,状态,first1 [1],last1 [1]);

在这里,您呼叫 addInformation 在主线程并按住返回值变量。但问题是,你的添加信息的实际实现在 doInBackground 的AsyncTask 的实际上确实在完成工作工作线程(异步调用),所以你总是会得到一个= 0的默认值(作为长)。

您需要做的是你的code移至 onPostExecute 的AsyncTask 类似下面的功能:

 公共无效addInformation(最终字符串名称,最后字符串的天气,最终字符串DATE2,最后弦乐状态,最后弦乐timeIn,最后弦乐超时)
    {
        类ADDINFO扩展的AsyncTask&LT;弦乐,太虚,字符串&GT; {
            ProgressDialog负荷;            @覆盖
            在preExecute保护无效(){
                super.on preExecute();
                装载= ProgressDialog.show(WorkDetailsTable.this,请等待,空,真,真);
            }            @覆盖
            保护无效onPostExecute(String s)将{
                super.onPostExecute(多个);
                loading.dismiss();
                Toast.makeText(getApplicationContext()中,s,Toast.LENGTH_LONG).show();
                addWorkForce(SUB,NOP,NOH,一);
            }            @覆盖
            保护字符串doInBackground(字符串... PARAMS){                HashMap的&LT;字符串,字符串&GT;数据=新的HashMap&LT;字符串,字符串&GT;();
                data.put(Config.KEY_USER_NAME,名);
                data.put(Config.KEY_WEATHER,天气);
                data.put(Config.KEY_DATE,DATE2);
                data.put(Config.KEY_STATUS,地位);
                data.put(Config.KEY_TIMEIN,timeIn);
                data.put(Config.KEY_TIMEOUT,超时);
                RequestHandler RH =新RequestHandler();
                字符串结果= rh.sendPostRequest(Config.ADD_INFORMATION,数据);
                返回结果;
            }
        }         ADDINFO RU =新ADDINFO();
         ru.execute(姓名,天气,DATE2,地位,timeIn,超时);    }

有就是利用回调另一种方法。实现一个接口和你的AsyncTask完成后,您可以发送回调做另一份工作。

I have two table, one is Information and the other is WorkForce

Information

WorkForce

The twf column in WorkForce is used to get the id of Information, which suppose return as 1, but it return value 0. If the id in Information is 5, twf should be 5 too.

Firstly I use the a to represent the number of selected row and then add the parameter of a into addWorkForce. The toast always display 0 .

  a=addInformation(name,weather,date2,status,first1[1],last1[1]);
  Toast.makeText(getApplicationContext(),a+"",Toast.LENGTH_LONG).show();
  addWorkForce(Sub, NoP, NoH,a);

addInformation function

  public long addInformation( final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut)
    {
        class AddInfo extends AsyncTask<String, Void, String> {
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {

                HashMap<String, String> data = new HashMap<String,String>();
                data.put(Config.KEY_USER_NAME,name);
                data.put(Config.KEY_WEATHER,weather);
                data.put(Config.KEY_DATE,date2);
                data.put(Config.KEY_STATUS,status);
                data.put(Config.KEY_TIMEIN,timeIn);
                data.put(Config.KEY_TIMEOUT,timeOut);
                RequestHandler rh=new RequestHandler();
                String result = rh.sendPostRequest(Config.ADD_INFORMATION,data);
                return  result;
            }
        }

         AddInfo ru = new AddInfo();
         ru.execute(name,weather,date2,status,timeIn,timeOut);

        return 0;

    }

Can someone help me to figure out the problem ?

addInformation.php

<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){

        //Getting values
        $name = $_POST['name'];
        $weather = $_POST['weather'];
        $date = $_POST['date'];
                $status = $_POST['status'];
                $timeIn = $_POST['timeIn'];
                $timeOut = $_POST['timeOut'];

        //Creating an sql query
        $sql = "INSERT INTO information(name, weather, date, status, time_in, time_out) VALUES ('$name','$weather','$date', '$status', '$timeIn', '$timeOut')";

        //Importing our db connection script
        require_once('dbConnect.php');

        //Executing query to database
        if(mysqli_query($con,$sql)){
            echo 'Information Added Successfully';
        }else{
            echo 'Could Not Add Information';
        }

        //Closing the database 
        mysqli_close($con);
    }
?>

This is how I used the code to get the row inserted into sqlite and it works

 a = ts.insertTimeSheet(name, weather, date2, status, first1[1], last1[1]);

TimeSheet

 public long insertTimeSheet(String name, String weather, String date, String status, String TimeIn, String TimeOut) {
        database = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MyDatabaseHelper.Name, name);
        values.put(MyDatabaseHelper.Weather, weather);
        values.put(MyDatabaseHelper.Date, date);
        values.put(MyDatabaseHelper.Status, status);
        values.put(MyDatabaseHelper.TimeIn_Info, TimeIn);
        values.put(MyDatabaseHelper.TimeOut_Info, TimeOut);
        return database.insert(MyDatabaseHelper.TABLE_INFO, null, values); // if the id in Information is 5, twf display 5 too

解决方案

As per your code to add information:

a=addInformation(name,weather,date2,status,first1[1],last1[1]);

Here, you are calling addInformation on main thread and hold the return value to variable a. But the problem is, your actual implementation of adding information is done in doInBackground of AsyncTask which actually does the job on a worker thread (async call) and so you will always get the default value for a=0 (as a is long).

What you need to do is to move your code to onPostExecute function of AsyncTask like below:

public void addInformation( final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut)
    {
        class AddInfo extends AsyncTask<String, Void, String> {
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
                addWorkForce(Sub, NoP, NoH,a);
            }

            @Override
            protected String doInBackground(String... params) {

                HashMap<String, String> data = new HashMap<String,String>();
                data.put(Config.KEY_USER_NAME,name);
                data.put(Config.KEY_WEATHER,weather);
                data.put(Config.KEY_DATE,date2);
                data.put(Config.KEY_STATUS,status);
                data.put(Config.KEY_TIMEIN,timeIn);
                data.put(Config.KEY_TIMEOUT,timeOut);
                RequestHandler rh=new RequestHandler();
                String result = rh.sendPostRequest(Config.ADD_INFORMATION,data);
                return  result;
            }
        }

         AddInfo ru = new AddInfo();
         ru.execute(name,weather,date2,status,timeIn,timeOut);

    }

There is another approach to use callbacks. Implement an interface and after completion of your AsyncTask, you can send callback to do another job.

这篇关于值始终返回0,它应该返回插入的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆