SQLite:当插入工作正常时从查询中获取值 0
SQLite: Getting value 0 from a query when insertions work correctly
我有一个 Table Notes (_Id, Title, Body, Category)
和一个 Table Categories (_Id, Name)
,引用类别 Categories._Id。
通过这种方法,我在我的 SQLite 数据库中创建了一个注释。
/**
* Create a new note using the title and body provided, and associated to
* the category with row id provided. If the note is successfully created
* return the new rowId for that note, otherwise return a -1 to indicate failure.
*
* @param title the title of the note
* @param body the body of the note
* @param category the category associated to the note
* @return rowId or -1 if failed
*/
public long createNote(String title, String body, String category) {
if (title == null || title.equals("") || body == null) {
return -1;
}
else {
ContentValues initialValues = new ContentValues();
initialValues.put(NOTE_KEY_TITLE, title);
initialValues.put(NOTE_KEY_BODY, body);
// If it has associated a category
if (!category.equals("No Category")) {
Cursor idCategory = fetchCategory(category);
long catId = idCategory.getLong(idCategory.getColumnIndex("_id"));
initialValues.put(NOTE_KEY_CAT, catId);
// Else, it has no category
} else {
initialValues.put(NOTE_KEY_CAT, (Byte) null);
}
return mDb.insert(DATABASE_NOTE_TABLE, null, initialValues);
}
}
有了这个,我得到了我数据库中的所有笔记。
/**
* Return a Cursor over the list of all notes in the database
*
* @param mode - if TITLE, it returns the list ordered by the title of the notes.
* - if CATEGORY, it returns the list ordered by the category of the notes.
* - it returns null in another case.
* @param category - category of the notes to return (No Category if none)
* @return Cursor over all notes
*/
public Cursor fetchAllNotes(String mode, String category) {
//// Order
String order;
// Mode is Title
if (mode.equals("TITLE")) {
order = NOTE_KEY_TITLE;
// Mode is Categories
} else if (mode.equals("CATEGORY")) {
order = CAT_KEY_NAME;
// A forbidden mode
} else {
return null;
}
// No category filter if it is No Category
String cat;
if (category.equals("No Category")) {
cat = "";
} else {
cat = " WHERE " + CAT_KEY_NAME + "='" + category + "'";
} // Left outer because there are notes without category
String query = "SELECT * FROM " + DATABASE_NOTE_TABLE + " LEFT OUTER JOIN " + DATABASE_CAT_TABLE +
" C ON " + NOTE_KEY_CAT + "=C." + CAT_KEY_ROWID + cat + " ORDER BY " + order;
return mDb.rawQuery(query, null);
}
编辑:
我实现了几个createNote()
操作,并且注释被正确插入(方法returns创建的注释的正确rowId [1, 2, ...] ).
我意识到从fetchAllNotes()
操作,所有的笔记都被返回了。但是,带有 No Category
的注释的 rowId 属性为 0(当 createNote()
返回正值时)。带有类别的注释具有正 rowId 值,但不正确。
例如:
CreateNote (Title: A, Body: B, Category: No Category). Returns 1
CreateNote (Title: C, Body: D, Category: No Category). Returns 2
CreateNote (Title: E, Body: F, Category: I have a Category). Returns 3
FetchAllNotes() Returns:
1. Note(0, A, B, No Category)
2. Note(0, C, D, No Category)
3. Note(1, E, F, I have a Category)`
知道发生了什么吗?
将 *
替换为完整的字段列表(这是最佳实践)。
所以要 return 只有 Cursor 中需要的字段。
我有一个 Table Notes (_Id, Title, Body, Category)
和一个 Table Categories (_Id, Name)
,引用类别 Categories._Id。
通过这种方法,我在我的 SQLite 数据库中创建了一个注释。
/**
* Create a new note using the title and body provided, and associated to
* the category with row id provided. If the note is successfully created
* return the new rowId for that note, otherwise return a -1 to indicate failure.
*
* @param title the title of the note
* @param body the body of the note
* @param category the category associated to the note
* @return rowId or -1 if failed
*/
public long createNote(String title, String body, String category) {
if (title == null || title.equals("") || body == null) {
return -1;
}
else {
ContentValues initialValues = new ContentValues();
initialValues.put(NOTE_KEY_TITLE, title);
initialValues.put(NOTE_KEY_BODY, body);
// If it has associated a category
if (!category.equals("No Category")) {
Cursor idCategory = fetchCategory(category);
long catId = idCategory.getLong(idCategory.getColumnIndex("_id"));
initialValues.put(NOTE_KEY_CAT, catId);
// Else, it has no category
} else {
initialValues.put(NOTE_KEY_CAT, (Byte) null);
}
return mDb.insert(DATABASE_NOTE_TABLE, null, initialValues);
}
}
有了这个,我得到了我数据库中的所有笔记。
/**
* Return a Cursor over the list of all notes in the database
*
* @param mode - if TITLE, it returns the list ordered by the title of the notes.
* - if CATEGORY, it returns the list ordered by the category of the notes.
* - it returns null in another case.
* @param category - category of the notes to return (No Category if none)
* @return Cursor over all notes
*/
public Cursor fetchAllNotes(String mode, String category) {
//// Order
String order;
// Mode is Title
if (mode.equals("TITLE")) {
order = NOTE_KEY_TITLE;
// Mode is Categories
} else if (mode.equals("CATEGORY")) {
order = CAT_KEY_NAME;
// A forbidden mode
} else {
return null;
}
// No category filter if it is No Category
String cat;
if (category.equals("No Category")) {
cat = "";
} else {
cat = " WHERE " + CAT_KEY_NAME + "='" + category + "'";
} // Left outer because there are notes without category
String query = "SELECT * FROM " + DATABASE_NOTE_TABLE + " LEFT OUTER JOIN " + DATABASE_CAT_TABLE +
" C ON " + NOTE_KEY_CAT + "=C." + CAT_KEY_ROWID + cat + " ORDER BY " + order;
return mDb.rawQuery(query, null);
}
编辑:
我实现了几个
createNote()
操作,并且注释被正确插入(方法returns创建的注释的正确rowId [1, 2, ...] ).我意识到从
fetchAllNotes()
操作,所有的笔记都被返回了。但是,带有No Category
的注释的 rowId 属性为 0(当createNote()
返回正值时)。带有类别的注释具有正 rowId 值,但不正确。
例如:
CreateNote (Title: A, Body: B, Category: No Category). Returns 1
CreateNote (Title: C, Body: D, Category: No Category). Returns 2
CreateNote (Title: E, Body: F, Category: I have a Category). Returns 3
FetchAllNotes() Returns:
1. Note(0, A, B, No Category)
2. Note(0, C, D, No Category)
3. Note(1, E, F, I have a Category)`
知道发生了什么吗?
将 *
替换为完整的字段列表(这是最佳实践)。
所以要 return 只有 Cursor 中需要的字段。