Android中的SQLite查询无法正常工作

时间:2015-09-15 12:39:53

标签: android sqlite

我正在尝试编写一个查询,但是我的应用程序崩溃了,我没有看到原因,我有这样的功能

<s:iterator value="objlistwitharry" >
    <s:property value="id" /><br/>
    <s:property value="name" /><br/>
    <s:property value="role" />
</s:iterator>

如您所见,我尝试了查询和rawQuery版本都无法正常工作。 “Sat”和“Dan”就是这样获得的。

public Cursor getPraktikum(long sat, String dan) throws SQLException {
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[]{COLUMN_NAME_NAME}, COLUMN_NAME_DAY + "=" + dan + "AND" + "(" + COLUMN_NAME_START + ">=" + sat + "AND" + COLUMN_NAME_END + "<" + sat + ")", null,
                    null, null, null, null); 
      // Cursor mCursor=db.rawQuery("SELECT" + COLUMN_NAME_NAME + "FROM " + DATABASE_TABLE + "WHERE" + COLUMN_NAME_DAY + "=" +dan + "AND" + "(" + COLUMN_NAME_START + ">=" + sat + "AND" + COLUMN_NAME_END + "<" + sat + ")", null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

我用自己的getTermin(long rowId)函数引导自己,这个函数就像这样工作

Calendar kalendar = Calendar.getInstance();
    int sat = kalendar.get(Calendar.HOUR_OF_DAY);
    int dani= kalendar.get(Calendar.DAY_OF_WEEK);
    String dan=new String();
    if(dani==3) {
        dan ="Utorak";
    }
    DatabaseHelper dbi=new DatabaseHelper(this.getApplicationContext());
    dbi.open();
  //  Cursor c=dbi.getTermin(1);
   Cursor cu = dbi.getPraktikum(sat,dan);
 /*   if (cu.moveToFirst())
        DisplayContact(cu); */
    dbi.close();
}
public void DisplayContact(Cursor c)
{
    Toast.makeText(this,
            "id: " + c.getString(0) + "\n",

            Toast.LENGTH_LONG).show();
}

我已经google了一切,但我似乎无法找到为什么功能getPraktikum不工作和getTermin,请帮忙!

2 个答案:

答案 0 :(得分:1)

显然,您没有正确地编写查询。你需要在字符串中添加空格

db.query(true, DATABASE_TABLE, new String[]{COLUMN_NAME_NAME}, COLUMN_NAME_DAY + "=" + dan + "AND" + "(" + COLUMN_NAME_START + ">=" + sat + "AND" + COLUMN_NAME_END + "<" + sat + ")", null,
                    null, null, null, null); 

尝试更改,&#34; AND&#34;致:

COLUMN_NAME_DAY + " = " + dan + " AND " ~~ 

答案 1 :(得分:0)

在SQL中,必须引用字符串值:

... WHERE day = 'Utorak' ...

但是,当字符串包含引号时,以这种方式使用字符串会导致格式化问题。 更好的方法是使用参数:

db.query(true, DATABASE_TABLE, new String[]{COLUMN_NAME_NAME},
         COLUMN_NAME_DAY + "= ? AND" +
         "(" + COLUMN_NAME_START + ">=" + sat + " AND " +
               COLUMN_NAME_END   + "<"  + sat + ")",
         new String[]{ dan },
         null, null, null, null);