0

我的查询select where子句有问题sqlite。当我尝试将数据textView与之匹配时,database它不会返回匹配的数据。

我的查询功能

public void cekPosisi(String txtAlamat) {
    String add = "";
    String alarm = "";
    Cursor cur = db.rawQuery("select * from task_table where alamat = '" + 
            txtAlamat + "'", null);
    if(cur.moveToFirst()) {
        add = cur.getString(5);
        alarm = "Anda ada tugas di lokasi: " + add;
        for (; !cur.isAfterLast(); cur.moveToNext()) {
            add = cur.getString(5);
            alarm = "Anda ada tugas di lokasi: " + add;
        }
    }
    if(add.equals("")) {
        add = "Anda tidak ada tugas di lokasi sekarang";
        alarm = "" + add;
    }
    Toast.makeText(this, alarm, Toast.LENGTH_LONG).show();
}

实施于

//cek tugas
    this.cariTugas.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            txtAddr = teksAlamat.getText().toString();
            Log.d("alamat textView", txtAddr);
            cekPosisi(txtAddr);
        }
    });

在第一个代码中,它总是运行到if(add.equals(""))条件,所以它会显示“Anda tidak ada tugas di lokasi sekarang”......请帮助。谢谢

4

2 回答 2

0
//cek tugas
    this.cariTugas.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            txtAddr = teksAlamat.getText().toString().trim();
            Log.d("alamat textView", txtAddr);
            if(txtAddr != null && !txtAddr.equals("")){
                cekPosisi(txtAddr);
            }else{
                Log.d("","Please enter value again");
            }
          }
      });


//if you have data in the database then you will have values accordingly
于 2013-04-09T09:43:19.047 回答
0

使用如下:

Cursor cur = db.rawQuery("select * from task_table where alamat = '%" + 
        txtAlamat + "%'", null);

检查此链接有 LIKE Query 的示例。并且 hou 在 Query 中使用通配符(模式中缺少字母)。

编辑:

    Cursor cursor = db.rawQuery("select * from task_table where alamat = '%" + 
            txtAlamat + "%'", null);
    if(cursor){
        Toast.makeText(this, "no data", Toast.LENGTH_LONG).show();
    }
    if (cursor.moveToFirst()) {
                do {
                      add = cur.getString(5);
                      if(!add.equalsIgnoreCase("")){
                         alarm = "Anda ada tugas di lokasi: " + add;
                      }else{
                         add = "Anda tidak ada tugas di lokasi sekarang";
                         alarm = "" + add;
                      }
                } while (cursor.moveToNext());
            }
于 2013-04-09T10:10:22.063 回答