我已设置以下光标以填充包含用户付款记录的对话框
Cursor PaymentsCursor = db.getReadableDatabase().rawQuery(
"SELECT _id, Date, Payment FROM tblPaymentHistory WHERE DebtName = '"
+ debtname + "'" + "ORDER BY _id ASC", null);
SimpleCursorAdapter HistoryAdapter = new SimpleCursorAdapter(this,
R.layout.paymenthistoryrow, PaymentsCursor, from, to);
问题是,如果存在多种类型的债务,并且无偿地对每个债务进行支付,当支付历史记录返回结果时,它将作为无序行数返回,例如1,2,6,7,9,12等。我知道它从数据库中提取_id(唯一键),但有没有办法在查询中重新定位或更改行号,以便每个结果返回“1,2,3,4,5等” “不管原始身份证?
我认为ORDER BY _id甚至ORDER BY Date ASC会解决这个问题,但事实并非如此。
我在数据库中的行看起来像这样:
1,TEST,4/13 / 2012,250
2,TEST,4/13 / 2012,300
3,TEST,4/14 / 2012,222
4,TEST2,4 / 14 / 2012,500
5,TEST,4/15 / 2012,600
当用户点击“TEST”的历史记录时,它返回1,2,3,5 ...如果他们拉出“TEST2”的历史记录,则显示为“4”,我正在尝试得到它所以TEST显示“1,2,3,4”,TEST2显示“1”
该死的我无法回答我自己的答案,但这就是我最终做的事情:
谢谢你们。我发现了一个修改视图的备用选项,因此无需触及SqLite数据库。继承了我找到Modifying SimpleCursorAdapter's data
的链接结果如下:
PaymentsCursor = db.getReadableDatabase().rawQuery(
" SELECT _id, Date, Payment FROM tblPaymentHistory WHERE DebtName = '"
+ debtname + "'" + "ORDER BY _id ASC", null);
String[] from = new String[] { DbAdapter.KEY_HISTORY_ID,
DbAdapter.HISTORY_DATE, DbAdapter.HISTORY_PAYMENT };
int[] to = new int[] { R.id.PAYMENTNO, R.id.PAYMENTDATE,
R.id.PAYMENTAMOUNT };
SimpleCursorAdapter HistoryAdapter = new SimpleCursorAdapter(this,
R.layout.paymenthistoryrow, PaymentsCursor, from, to);
HistoryAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int column) {
if (column == 0) { // let's suppose that the column 0 is the
// date
TextView tv = (TextView) view;
String rownum = String.valueOf(cursor.getPosition() + 1);
// here you use SimpleDateFormat to bla blah blah
tv.setText(rownum);
return true;
}
return false;
}
});
paymenthistory.setAdapter(HistoryAdapter);
它可能不是最富有魅力的方式,但现在每次窗口出现历史记录时,它都使用行号(加一)来表示它是哪个#。 谢谢大家!
答案 0 :(得分:1)
这是获得“重新基础”ID的一种方法。在此示例中,“新ID”基于grade
(即您的情况中的“旧ID”):
.headers on
create table foo (name text, grade int);
insert into foo values ('Joe', 45);
insert into foo values ('Anna', 98);
insert into foo values ('Julie', 78);
select name,
grade,
(select count(*) from foo t1 where t1.grade>=t2.grade) as rank
from foo t2;
select name,
grade,
(select count(*) from foo t1 where t1.grade>=t2.grade) as rank
from foo t2
order by rank;
将此保存为foo.sql后,我得到了这个:
[someone@somewhere tmp]$ sqlite3 < foo.sql
name|grade|rank
Joe|45|3
Anna|98|1
Julie|78|2
name|grade|rank
Anna|98|1
Julie|78|2
Joe|45|3
答案 1 :(得分:0)
我已经玩了@sixfeetsix所回答的内容,因为它没有给出1,2,3,4,..编号与WHERE结合你可能需要放入更多子查询(也许不是,但是,我对查询不太满意:
SELECT (
SELECT count( * ) + 1
FROM (
SELECT *
FROM tblPaymentHistory
WHERE DebtName = ?
)
AS t1
WHERE t1._id < t2._id
)
AS _id,
Date,
Payment
FROM tblPaymentHistory AS t2
WHERE DebtName = ?
ORDER BY _id;
放入java String并将?
留在那里以获取转义值(注入安全):
Cursor PaymentsCursor = db.getReadableDatabase().rawQuery(
"...WHERE DebtName=? .. WHERE DebtName=? .. ", new String[]{ debtname, debtname });
SimpleCursorAdapter HistoryAdapter = new SimpleCursorAdapter(this,
R.layout.paymenthistoryrow, PaymentsCursor, from, to);
答案 2 :(得分:0)
经过长时间的研究后,这个查询对我有用......
我得出的实证结果:
1)您还需要在子查询中定义条件。
2)如果在关系运算符中比较的id(t2._id&lt; = t1._id)将是主键,那么它
在所有情况下都能正常工作。
3)关于订单条件,您必须根据您的选择或需要决定。
SELECT
(SELECT COUNT(*) FROM table t2 WHERE t2._id <= t1._id AND t2.Recipe_id = 2) AS RowNumber,_id,Recipe_id,col2,col3,col4
FROM table t1
WHERE Recipe_id = 2
ORDER BY _id
工作原理: -
假设我们在某些表格中有一系列主键1,2,3,4,5,6
现在我们使用表t1和表t2
创建它的两个别名Now both have same sequence table t1 -> 11,12,13,14,15,16
table t2 -> 11,12,13,14,15,16
现在这个条件( WHERE t2._id <= t1._id )
将t2的第一个主键“11”与
t2 as 11=11
的第一个主键“11”将return count()
仅存在一行,因此我们get "1" in row number
..
*** remember for every row in Outer query the sub-query is executed ***
因此now outer query is at row second having primary key "12"
现在again compare the ( WHERE t2._id <= t1._id )
again t2._id contains "11"
t1._id contains "12"
TWO rows are there
。{/ 1>}
很安静,它会返回ids <= 12
{{1}},即11和12
这样它就会产生所需的序列.........
这是一个生成序列的简单技巧..实际上并不简单,但是当你深入了解它时非常简单..
我不是专家,但这是我理解的...... 希望解释有所帮助......
由于网上有各种解决方案或相同的解决方案,但没有解释..
:)