gpt4 book ai didi

java - 从后向读取Sql数据库

转载 作者:行者123 更新时间:2023-12-01 12:48:41 25 4
gpt4 key购买 nike

嘿伙计,我有一个问题。我怎样才能从后面读取我的Sql数据。我尝试了很多在网上找到的东西,但我的代码没有任何作用:

public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null,null,null);
if (c != null) {
c.moveToFirst();
}
return c;

以及光标的代码:

Cursor cursor = myDb.getAllRows();

// Allow activity to manage lifetime of the cursor.
// DEPRECATED! Runs on the UI thread, OK for small/short queries.
startManagingCursor(cursor);

// Setup mapping from cursor to view fields:
String[] fromFieldNames = new String[]
{DBAdapter.KEY_KALO, DBAdapter.KEY_HYDRATE, DBAdapter.KEY_FAT, DBAdapter.KEY_PROTEIN ,DBAdapter.KEY_CAL};
int[] toViewIDs = new int[]
{R.id.item_Kalorien, R.id.item_KG_in_g, R.id.item_F_in_g, R.id.item_P_in_g,R.id.item_cal};

// Create adapter to may columns of the DB onto elemesnt in the UI.
SimpleCursorAdapter myCursorAdapter =
new SimpleCursorAdapter(
this, // Context
R.layout.item_tagebuch, // Row layout template
cursor, // cursor (set of DB records to map)
fromFieldNames, // DB Column names
toViewIDs // View IDs to put information in
);

// Set the adapter for the list view
ListView myList = (ListView) findViewById(R.id.listView1);
myList.setAdapter(myCursorAdapter);

谢谢你的帮助,很抱歉我的英语不好:)

最佳答案

“从后面”的说法不明确,我假设您想以特定顺序读取数据库

如果您想按字段的降序阅读,请说“foo”,然后

String ORDER = "foo DESC";/*使用 ASC 升序*/

并按如下方式更改 cod

public Cursor getAllRows() {
String where = null;
String Order = "foo DESC";
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null,Order,null);
if (c != null) {
c.moveToFirst();
}
return c;

这是查询的语法

公共(public)游标查询(字符串表、字符串[]列、字符串选择、字符串[]selectionArgs、字符串 groupBy、字符串having、字符串 orderBy、字符串 limit)

请参阅以下链接以获取进一步说明 SQLiteDatabase Android

关于java - 从后向读取Sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24429553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com