值总是返回 0,它应该返回插入的行数

Posted

技术标签:

【中文标题】值总是返回 0,它应该返回插入的行数【英文标题】:Value always return 0 where it should return number of row inserted 【发布时间】:2016-04-04 16:46:14 【问题描述】:

我有两张桌子,一张是Information,另一张是WorkForce

信息

劳动力

WorkForce中的twfcolumn用于获取Informationid,假设返回为1,但返回值为0。如果@ 信息中的 987654330@ 是 5,twf 也应该是 5。

首先我使用a 来表示选中的行数,然后将a 的参数添加到addWorkForce 中。 toast 总是显示 0 。

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

添加信息功能

  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;

    

有人可以帮我解决问题吗?

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);
    
?>

这就是我使用代码将行插入到 sqlite 中的方式,它可以工作

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

时间表

 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

【问题讨论】:

echo 'Could Not Add Information'; 如果您的查询中有错误,John 不会帮助您。你需要得到真正的错误。如果有的话,添加or die(mysqli_error($con))mysqli_query() @Fred-ii- 我的代码没有错误。我只想知道如何将表格Info的行数获取到WorkForce 它应该返回 1 到列 workForce。如果有第 2 行,则在 workForce twf 中显示 2... “我只想知道如何获取行数”(根据其他地方的评论)。这似乎是与 android 相关的语法,我对此无能为力。在 SQL 中,您可以尝试使用 mysqli_affected_rows() php.net/manual/en/mysqli.affected-rows.php 但我不完全确定这是否是您的问题所在。 调试它,它在完成AsyncTask之前返回值为0 【参考方案1】:

我相信问题出在这条线上

return 0;

您的 addInformation 方法每次都返回值为 0 的 long。如果您希望它返回其他数字,则需要更改该行。

编辑: 要跟踪您的行,请在方法标题中添加另一个参数

public long addInformation(int rowId, final String name, final String weather, final String date2, final String status, final String timeIn, final String timeOut)

【讨论】:

我应该使用什么值? 无论您期望它返回什么值。如果您想要行的 id,则需要将其作为字段添加到您的类中,以便它可以成为构造函数的一部分,然后用于在方法结束时返回。 将twf设为foreign key ? 这是在 SQL 表中获取正确数字的一种方法,但这并不能解决 Toast 通知始终为 0 的问题 你能告诉我如何将 id 添加为我班级的字段吗?【参考方案2】:

你可以在第一个表中插入行之后使用mysql函数lastInsertId()。即(信息) 将 lastInsertId() 的值存储在某个变量中,并在您在下一个表中插入行时使用该变量。那就是(劳动力)

【讨论】:

你能详细说明一下吗?【参考方案3】:

由于AsyncTask 是一个异步任务,所以addInformation 方法中的return 0; 行总是排在第一位,这就是为什么你得到0 而不是"的原因行插入”

你应该更新你的代码如下,请关注我的评论// MOVE addWorkForce HERE

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, Long.parseLong(s)); // MOVE addWorkForce HERE
        

        @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);  

那么,你只需要调用

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

当然,请确保 Sub, NoP, NoH 变量可在 AddInfo 类中访问。

希望对你有帮助!

【讨论】:

所以使用s 而不是a ? 是的,因为return result;doInBackground 将是sonPostExecute :) 您可以在developer.android.com/reference/android/os/AsyncTask.htmlThe 4 steps 部分阅读更多关于 AsyncTask 的信息 好的,但是我的第四个参数类型错误,因为我的s 数据类型是字符串:/ 啊,那你可以用Integer.parseInt(s),不过,应该也能赶上NumberFormatException【参考方案4】:

根据您的代码添加信息:

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

在这里,您在主线程上调用addInformation 并将返回值保存到变量a。但问题是,您添加信息的实际实现是在AsyncTaskdoInBackground 中完成的,它实际上在工作线程(异步调用)上完成了这项工作,因此您将始终获得 a=0 的默认值(作为很长)。

您需要做的是将您的代码移动到AsyncTaskonPostExecute 函数,如下所示:

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);

    

还有另一种使用回调的方法。实现一个接口,在你的 AsyncTask 完成后,你可以发送回调来做另一个工作。

【讨论】:

以上是关于值总是返回 0,它应该返回插入的行数的主要内容,如果未能解决你的问题,请参考以下文章

mysql不是自动增长的主键怎么利用ibatis返回值判断是不是插入成功

c#: ExecuteNonQuery() 返回 -1

SQL Server返回插入数据的ID和受影响的行数

SQL Server返回插入数据的ID和受影响的行数

如何在PHP中获取MYSQL数据库返回的数据的行数?

c#如何返回插入行的ID值