Мой код выглядит так:
public static synchronized String getPreferenceString(Context context, String key)
{
Cursor c = DatabaseHelper.getDatabase(context).query(TABLE_NAME, new String[]{ "Value" }, "Key=?", new String[]{ key }, null, null, null, null);
if (c.getCount() == 0)
{
c.close();
return "";
}
c.moveToFirst();
String retVal = c.getString(0);
c.close();
return retVal;
}
По сути, я получаю конкретное значение из таблицы базы данных.Он отлично работает, но я хочу предварительно оптимизировать свой код, чтобы он выглядел так:
public static synchronized String getPreferenceString(Context context, String key)
{
Cursor c = DatabaseHelper.getDatabase(context).query(TABLE_NAME, new String[]{ "Value" }, "Key=?", new String[]{ key }, null, null, null, null);
if (c.getCount() == 0) return "";
c.moveToFirst();
return c.getString(0);
}
Это нормально, или я должен закрыть эти курсоры?