Cursor.moveToNext顺序问题?

本文探讨了一个关于数据库中RowID遍历的意外发现:遍历并非从RowID=1开始,而是从最后一个RowID向下遍历,直至RowID=1。这一特性对于理解数据库查询优化及性能调整具有重要意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今用的时候偶然发现,我以前认为它是先从rowid=1开始遍历的,然而不是,是从最后,rowid=最后 开始,然后向上逐级遍历,最后才是遍历rowid=1。

我现在还在纳闷儿为什么,然而事实就是如此。

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
package cn.itcast.notepad; //package com.example.helloworld; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; public class NoteRepository { private DatabaseHelper dbHelper; public NoteRepository(Context context) { dbHelper = new DatabaseHelper(context); } public long insert(Note note) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("title", note.getTitle()); values.put("content", note.getContent()); long id = db.insert(DatabaseHelper.TABLE_NOTES, null, values); db.close(); return id; } public List<Note> getAllNotes() { List<Note> notes = new ArrayList<>(); SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query(DatabaseHelper.TABLE_NOTES, null, null, null, null, null, null); while (cursor.moveToNext()) { long id = cursor.getLong(cursor.getColumnIndex(DatabaseHelper.COLUMN_ID)); String title = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_TITLE)); String content = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_CONTENT)); notes.add(new Note(id, title, content)); } cursor.close(); db.close(); return notes; } public void update(Note note) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("title", note.getTitle()); values.put("content", note.getContent()); db.update(DatabaseHelper.TABLE_NOTES, values, DatabaseHelper.COLUMN_ID + " = ?", new String[]{String.valueOf(note.getId())}); db.close(); } public void delete(long id) { SQLiteDatabase db = dbHelper.getWritableDatabase();
03-13
// 位置: java/com/example/todoapp/DatabaseHelper.java public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "todo.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_TASKS = "tasks"; private static final String COLUMN_ID = "id"; private static final String COLUMN_TITLE = "title"; // 其他列定义... public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_TABLE = "CREATE TABLE " + TABLE_TASKS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TITLE + " TEXT," + "description TEXT," + "is_completed INTEGER DEFAULT 0," + "category TEXT," + "timestamp INTEGER)"; db.execSQL(CREATE_TABLE); } // 添加任务 public void addTask(Task task) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_TITLE, task.getTitle()); values.put("description", task.getDescription()); values.put("category", task.getCategory()); values.put("timestamp", task.getTimestamp()); db.insert(TABLE_TASKS, null, values); db.close(); } // 获取所有任务 public List<Task> getAllTasks() { List<Task> taskList = new ArrayList<>(); String selectQuery = "SELECT * FROM " + TABLE_TASKS; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { Task task = new Task( cursor.getString(1), cursor.getString(2), cursor.getString(4) ); task.setId(cursor.getInt(0)); task.setCompleted(cursor.getInt(3) == 1); taskList.add(task); } while (cursor.moveToNext()); } cursor.close(); return taskList; } // 更新任务状态/删除任务等方法(此处省略) } 刚才就是这段代码报了上面的错误,大面积的No candidates found for method call task.getTitle().报错或No candidates found for method call cursor.getString(2).这个报错,请帮我检查并尝试在不改变源程序的情况下修改
06-18
package com.example.myapplication; import android.database.Cursor; import androidx.room.EntityDeletionOrUpdateAdapter; import androidx.room.EntityInsertionAdapter; import androidx.room.RoomDatabase; import androidx.room.RoomSQLiteQuery; import androidx.room.util.CursorUtil; import androidx.room.util.DBUtil; import androidx.sqlite.db.SupportSQLiteStatement; import java.lang.Class; import java.lang.Override; import java.lang.String; import java.lang.SuppressWarnings; import java.util.ArrayList; import java.util.Collections; import java.util.List; import javax.annotation.processing.Generated; @Generated("androidx.room.RoomProcessor") @SuppressWarnings({"unchecked", "deprecation"}) public final class BookmarkDao_Impl implements BookmarkDao { private final RoomDatabase __db; private final EntityInsertionAdapter<Bookmark> __insertionAdapterOfBookmark; private final EntityDeletionOrUpdateAdapter<Bookmark> __deletionAdapterOfBookmark; public BookmarkDao_Impl(RoomDatabase __db) { this.__db = __db; this.__insertionAdapterOfBookmark = new EntityInsertionAdapter<Bookmark>(__db) { @Override public String createQuery() { return "INSERT OR ABORT INTO `bookmarks` (`id`,`bookId`,`position`,`createTime`,`note`) VALUES (nullif(?, 0),?,?,?,?)"; } @Override public void bind(SupportSQLiteStatement stmt, Bookmark value) { stmt.bindLong(1, value.getId()); if (value.getBookId() == null) { stmt.bindNull(2); } else { stmt.bindString(2, value.getBookId()); } stmt.bindLong(3, value.getPosition()); stmt.bindLong(4, value.getCreateTime()); if (value.getNote() == null) { stmt.bindNull(5); } else { stmt.bindString(5, value.getNote()); } } }; this.__deletionAdapterOfBookmark = new EntityDeletionOrUpdateAdapter<Bookmark>(__db) { @Override public String createQuery() { return "DELETE FROM `bookmarks` WHERE `id` = ?"; } @Override public void bind(SupportSQLiteStatement stmt, Bookmark value) { stmt.bindLong(1, value.getId()); } }; } @Override public void insert(final Bookmark bookmark) { __db.assertNotSuspendingTransaction(); __db.beginTransaction(); try { __insertionAdapterOfBookmark.insert(bookmark); __db.setTransactionSuccessful(); } finally { __db.endTransaction(); } } @Override public void delete(final Bookmark bookmark) { __db.assertNotSuspendingTransaction(); __db.beginTransaction(); try { __deletionAdapterOfBookmark.handle(bookmark); __db.setTransactionSuccessful(); } finally { __db.endTransaction(); } } @Override public List<Bookmark> getBookmarksByBook(final String bookId) { final String _sql = "SELECT * FROM bookmarks WHERE bookId = ?"; final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1); int _argIndex = 1; if (bookId == null) { _statement.bindNull(_argIndex); } else { _statement.bindString(_argIndex, bookId); } __db.assertNotSuspendingTransaction(); final Cursor _cursor = DBUtil.query(__db, _statement, false, null); try { final int _cursorIndexOfId = CursorUtil.getColumnIndexOrThrow(_cursor, "id"); final int _cursorIndexOfBookId = CursorUtil.getColumnIndexOrThrow(_cursor, "bookId"); final int _cursorIndexOfPosition = CursorUtil.getColumnIndexOrThrow(_cursor, "position"); final int _cursorIndexOfCreateTime = CursorUtil.getColumnIndexOrThrow(_cursor, "createTime"); final int _cursorIndexOfNote = CursorUtil.getColumnIndexOrThrow(_cursor, "note"); final List<Bookmark> _result = new ArrayList<Bookmark>(_cursor.getCount()); while(_cursor.moveToNext()) { final Bookmark _item; final String _tmpBookId; if (_cursor.isNull(_cursorIndexOfBookId)) { _tmpBookId = null; } else { _tmpBookId = _cursor.getString(_cursorIndexOfBookId); } final int _tmpPosition; _tmpPosition = _cursor.getInt(_cursorIndexOfPosition); _item = new Bookmark(_tmpBookId,_tmpPosition); final int _tmpId; _tmpId = _cursor.getInt(_cursorIndexOfId); _item.setId(_tmpId); _item.createTime = _cursor.getLong(_cursorIndexOfCreateTime); final String _tmpNote; if (_cursor.isNull(_cursorIndexOfNote)) { _tmpNote = null; } else { _tmpNote = _cursor.getString(_cursorIndexOfNote); } _item.setNote(_tmpNote); _result.add(_item); } return _result; } finally { _cursor.close(); _statement.release(); } } @Override public Bookmark getBookmarkById(final int id) { final String _sql = "SELECT * FROM bookmarks WHERE id = ?"; final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1); int _argIndex = 1; _statement.bindLong(_argIndex, id); __db.assertNotSuspendingTransaction(); final Cursor _cursor = DBUtil.query(__db, _statement, false, null); try { final int _cursorIndexOfId = CursorUtil.getColumnIndexOrThrow(_cursor, "id"); final int _cursorIndexOfBookId = CursorUtil.getColumnIndexOrThrow(_cursor, "bookId"); final int _cursorIndexOfPosition = CursorUtil.getColumnIndexOrThrow(_cursor, "position"); final int _cursorIndexOfCreateTime = CursorUtil.getColumnIndexOrThrow(_cursor, "createTime"); final int _cursorIndexOfNote = CursorUtil.getColumnIndexOrThrow(_cursor, "note"); final Bookmark _result; if(_cursor.moveToFirst()) { final String _tmpBookId; if (_cursor.isNull(_cursorIndexOfBookId)) { _tmpBookId = null; } else { _tmpBookId = _cursor.getString(_cursorIndexOfBookId); } final int _tmpPosition; _tmpPosition = _cursor.getInt(_cursorIndexOfPosition); _result = new Bookmark(_tmpBookId,_tmpPosition); final int _tmpId; _tmpId = _cursor.getInt(_cursorIndexOfId); _result.setId(_tmpId); _result.createTime = _cursor.getLong(_cursorIndexOfCreateTime); final String _tmpNote; if (_cursor.isNull(_cursorIndexOfNote)) { _tmpNote = null; } else { _tmpNote = _cursor.getString(_cursorIndexOfNote); } _result.setNote(_tmpNote); } else { _result = null; } return _result; } finally { _cursor.close(); _statement.release(); } } public static List<Class<?>> getRequiredConverters() { return Collections.emptyList(); } } 在这段代码中, public void setCreateTime(long createTime) { this.createTime = createTime;应该添加在哪里
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值