SQL查询不从数据库中读取数据

时间:2012-07-04 18:34:06

标签: android sql

所以我有两个从数据库读取数据并将数据写入字符串的游标,但即使数据库存储了数据,游标也总是为空。我检查了SQLite管理器,该数据库包含存储的数据。 Log Cat表示名为“IF1”和“IF2”的日志已执行。 这是我的代码,我希望有人能找到什么问题。

String var;
final TextView t = (TextView) findViewById(R.id.textView2);
final TextView t2 = (TextView) findViewById(R.id.textView3);
Intent intent = getIntent();
var = intent.getStringExtra("datum");   

SQLiteDatabase db = openOrCreateDatabase ("MyDB", MODE_PRIVATE, null);

String q =" SELECT COALESCE (znak, '') as znak,COALESCE (sprava, '') as sprava, COALESCE(serija, '') as serija, COALESCE(podatak, '') as podatak, COALESCE(ponavljanja, '') as ponavljanja, vrijeme FROM tablica WHERE vrijeme LIKE '"+ var + "'" ;
Cursor c = db.rawQuery(q, null);      

String xy = "SELECT vrijeme FROM tablica WHERE vrijeme LIKE '"+ var + "'";
Cursor dd = db.rawQuery(xy, null);

if (dd.getCount() < 1) {
    Log.d("LIST.DETAILS", "IF1");
    dd.close();
    db.close();
    Toast.makeText(NapredakActivity.this, "Empty!", Toast.LENGTH_SHORT).show();
    startActivity(new Intent(this, calendar.class));
} else {
    String i = "";

    dd.moveToFirst();
    String st2 = dd.getString(dd.getColumnIndex("vrijeme"));
    i =  "Datum:" +"\t" + st2;
    t2.setText(i);
    dd.close(); 
}

if (c.getCount() < 1) {
    Log.d("LISTA.DETALJI", "IF2");
    c.close();
    db.close();
    Toast.makeText(NapredakActivity.this, "Empty!", Toast.LENGTH_SHORT).show();
    startActivity(new Intent(this, calendar.class));
} else {
    String j = "";

    c.moveToFirst();
    do {
        String cm55 = c.getString(c.getColumnIndex("sprava"));
        String cm = c.getString(c.getColumnIndex("serija"));
        String cm2 = c.getString(c.getColumnIndex("podatak"));
        String cm3 = c.getString(c.getColumnIndex("ponavljanja"));
        String cm_znak = c.getString(c.getColumnIndex("znak"));

        j = j +cm55+ "\n" + cm + "\t"  +cm2 +cm_znak + cm3 ;
    } while (c.moveToNext());

    t.setText(j);
    c.close();
    db.close();
}

1 个答案:

答案 0 :(得分:1)

我相信你缺少WHERE子句中的通配符:

"vrijeme LIKE '%"+ var + "%'"

否则:

"vrijeme LIKE '"+ var + "'"

与:

相同
"vrijeme = '"+ var + "'"

另外我猜测英语不是你的第一语言,但你的变量名字非常神秘。考虑使用更具描述性的名称,它只是有助于您的代码的可读性。