【问题标题】:How to Stop repeat Database in Android?如何在 Android 中停止重复数据库?
【发布时间】:2016-09-06 09:51:51
【问题描述】:

我是安卓新手。在这里我做了简单的服务。5 分钟后自动刷新。但问题是每次都重复数据库上传。所以帮助我....在我的代码中每次上传单个字符串,但我不会制作JSONArray。所以帮我看看如何在我的代码中制作JSONArray.....

public class One extends Service {
    public static final long NOTIFY_INTERVAL = 5*60*1000;
    private Handler handler = new Handler();
    private Timer timer = null;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        if(timer != null)
        {
            timer.cancel();
        }
        else
        {
            timer = new Timer();
        }
        timer.scheduleAtFixedRate(new DisplayTime(),0,NOTIFY_INTERVAL);
    }

    class DisplayTime extends TimerTask
    {

        @Override
        public void run() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    recll();
                    Toast.makeText(getApplicationContext(),"run",Toast.LENGTH_SHORT).show();
                }
            });
        }
        private void recll() {
            Uri uri = Uri.parse("content://sms/inbox");
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            if (cursor.moveToFirst()) {
                for (int i = 0; i < cursor.getCount(); i++) {
                    final String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
                    final String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
                    final String date = cursor.getString(cursor.getColumnIndexOrThrow("date")).toString();
                    Date date1 = new Date(Long.valueOf(date));
                    final String type = cursor.getString(cursor.getColumnIndexOrThrow("type")).toString();

                    final String fDate = date1.toString();

                    cursor.moveToNext();

                    class getSMSDetails extends AsyncTask<Void,Void,String>
                    {
                        @Override
                        protected String doInBackground(Void... params)
                        {
                            HashMap<String,String> param = new HashMap<String, String>();
                            param.put(Connect.KEY_NUMBER,number);
                            param.put(Connect.KEY_TYPE,type);
                            param.put(Connect.KEY_DATE,fDate);
                            param.put(Connect.KEY_BODY,body);

                            RequestHandler rh = new RequestHandler();
                            String res = rh.sendPostRequest(Connect.URL_ADD, param);
                            return res;
                        }
                    }

                    getSMSDetails idata = new getSMSDetails();
                    idata.execute();
                }
            }
            cursor.close();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

这是我的 PHP 代码.....这里我要求从输入时间开始输入 7 天的数据,每当有新条目出现时,我想在它再次输入整个数据时输入新条目,所以我想防止重复输入,谢谢提前……

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
   //Getting values
    $number = $_POST['number'];
    $type = $_POST['type'];
    $dates = $_POST['date'];
    $content = $_POST['content'];
    $start = strtotime("-7 days");
    $date = strtotime($dates);
    $end = strtotime("now");

    $query=mysqli_query($con,"SELECT * FROM message_detail where number =   '$number' and type = '$type' and date = '$date' and content = '$content'");

    if(mysqli_num_rows($query)>0) 
    {
        echo "already exist";
    }
    elseif($start <= $date &&  $end >= $date)
    {
        //Creating an sql query
        $sql = "INSERT IGNORE INTO message_detail (number,type,date,content) VALUES   ('$number','$type','$date','$content')";
    } 
    //Importing our db connection script
    require_once('connect.php');

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

连接.php

<?php

//Defining Constants
define('HOST','mysql.hostinger.in');
define('USER','u336100496_hiren');
define('PASS','');
define('DB','u336100496_sebu');

//Connecting to Database
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>

【问题讨论】:

  • 如果您真的需要任何帮助,查看 PHP 代码也会很有用
  • 感谢您的回复@RiggsFolly 很抱歉给您带来不便

标签: php android mysql arrays json


【解决方案1】:

将您的连接要求移至脚本顶部。

您在没有连接的情况下执行第一个查询,所以它总是会失败,因此会使用插入新行的 ELSE

另外,您在错误的地方运行 INSERT,它应该在 ELSEIF 中

<?php 
//Importing our db connection script
require_once('connect.php');

if($_SERVER['REQUEST_METHOD']=='POST'){
   //Getting values
    $number = $_POST['number'];
    $type = $_POST['type'];
    $dates = $_POST['date'];
    $content = $_POST['content'];
    $start = strtotime("-7 days");
    $date = strtotime($dates);
    $end = strtotime("now");

    $query=mysqli_query($con,"SELECT * 
                              FROM message_detail 
                              where number = '$number' 
                                and type = '$type' 
                                and date = '$date' 
                                and content = '$content'");

    if(mysqli_num_rows($query)>0) {
        echo "already exist";
    }
    elseif($start <= $date &&  $end >= $date) {
        //Creating an sql query
        $sql = "INSERT IGNORE INTO message_detail (number,type,date,content) VALUES   ('$number','$type','$date','$content')";

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

    } 

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

【讨论】:

  • 在 php logcat 中给出警告 mysqli_query(): Empty query
  • 您最好将connect.php 的代码添加到您的问题中
  • 无改动 require_once('connect.php'); php Warning 的 logcat:mysqli_query() 期望参数 1 为 mysqli,在 /home/u336100496/public_html/message_detail/insert.php14 中给出 null

    警告:mysqli_num_rows() 期望参数 1 为 mysqli_result,/home/u336100496/public_html/message_detail/insert.php 中给出的 null /b> 第 16 行

    警告:mysqli_query(): /home/u336100496/public_html/message_detail/insert 中的空查询第 29 行的 .php
    无法添加条目
  • 此处仅输入 7 天数据工作正常问题是防止重复输入
  • 所以你的连接失败了给我看你的connection.php脚本。编辑您的问题以添加它
猜你喜欢
  • 2014-02-08
  • 2018-12-29
  • 1970-01-01
  • 2014-11-30
  • 2013-12-24
  • 2012-01-21
  • 2016-07-23
  • 1970-01-01
  • 2016-12-27
相关资源
最近更新 更多