开发者

How can i convert this query into a rawQuery?

I have my database query currently set up开发者_JS百科 like so:

Edit, By the way my variables (_ID, CAT_ITEM and BUDGET_AMOUNT) actualy contain exactly the same text and formatting as they are written, silly i know, but anyway that shouldn't be the problem

private String[] from = {_ID, CAT_ITEM, BUDGET_AMOUNT, };
private String orderBy = _ID + " ASC";
private String whereIn = "IN_OUT='in'";

Cursor cursor = db.query(CAT_BUD_TAB, from, whereIn, null, null, null, orderBy);

Which does exactly what i want it to do, but i wanted to try using a rawQuery instead (just to get the hang of query statements and test it out, also i wouldn't need so many string variables).

Here's what i came up with:

private String incomeQuery = "SELECT _ID, CAT_ITEM, BUDGET_AMOUNT FROM CAT_BUD_TAB WHERE IN_OUT=’in’";
Cursor cursor = db.rawQuery(incomeQuery, null);

However this isn't working (I get the SQLException no such column) so i'm trying to work out what the difference is, I figure there's just something wrong with my statement but no idea what. Can anyone offer me any advice? Thanks


From your code it looks like _ID, CAT_ITEM, BUDGET_AMOUNT and CAT_BUD_TAB are String fields. Are their values the same as the field names?

Your single quotes also look different between the two queries.

Not related to your problem, but you will also need to add ORDER BY _ID ASC to the end of the sql to replicate the result order.


In your first example, you have the following:

private String[] from = {_ID, CAT_ITEM, BUDGET_AMOUNT, };

If this is correct, it means that you have three variables _ID, CAT_ITEM and BUDGET_AMOUNT with the names of the columns in your table. The same goes for CAT_BUD_TAB that probably holds the name of your table.

For your raw query to be correct, it should then be written like this:

String incomeQuery = "SELECT " + _ID + ", " + CAT_ITEM + ", " + BUDGET_AMOUNT + " FROM " + CAT_BUD_TAB + " WHERE IN_OUT=’in’";

And just to have said it: this is not a nice way to compose a string. It should instead be done using a StringBuilder. I just did it like this here for simplicity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜