我正在尝试检查用户输入的文本是否已经在我的数据库中,但不确定应该如何完成。
所以我可以通过在我的数据库助手类中调用以下命令从我的数据库中获取数据:
public Cursor fetchDamagedComponentsForLocation(long damagedComponentId) {
Cursor mCursor =
rmDb.query(true, DAMAGED_COMPONENTS_TABLE, new String[] {
COMPONENT_ID, LOCATION_LINK, RUN_LINK, AREA_LINK, INSPECTION_LINK, LOCATION_REF, RACKING_SYSTEM, COMPONENT, POSITION, RISK, ACTION_REQUIRED, NOTES_GENERAL, MANUFACTURER, TEXT1, TEXT2, TEXT3, TEXT4, NOTES_SPEC},
LOCATION_LINK + "=" + damagedComponentId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
对于每个数据库条目,我需要检查两个用户输入是否与用户单击保存按钮时从 Spinners 获得的两个数据库字段(在本例中为 COMPONENT 和 POSITION)匹配:
String component = ((Cursor)componentSpinner.getSelectedItem()).getString(2).toString();
String position = ((Cursor)positionSpinner.getSelectedItem()).getString(1).toString();
这基本上是为了确保尚未保存该组件。
我敢肯定这很简单,但我不知道该怎么做..
编辑 - 按照 Sams 的建议,我已将以下类添加到我的数据库助手类中:
public boolean checkIfComponentAlreadySaved(long locationId, String component, String position) {
Cursor mCursor = rmDb.query(DAMAGED_COMPONENTS_TABLE, new String[] {"1"},
LOCATION_LINK + " = " + locationId + " AND " + COMPONENT + " = " + component + " AND " + POSITION + " = " + position, null, null, null, null, null);
boolean result = mCursor.moveToFirst();
mCursor.close();
return result;
}
但是,我收到以下错误:
android.database.sqlite.SQLiteException:靠近“pin”:语法错误:,编译时:从损坏的组件表中选择 1 WHERE location_link = 3 AND component = Locking pin AND position = Not Applicable
我猜这是因为我正在比较字符串,但是有什么建议吗?