Would you mind pointing out what s wrong with this peace of code. genresCursor contains "Application did not close the cursor or database object that was opened here" exception. How do I really close this cursor after inserting ? Thanks.
UPD: 似乎完全没有问题。 尽管它包含了仍然可以提取数据的例外。 我实际上的应用程序中一定有错误, 而这个例外让我得出结论,那就是问题所在。 感谢大家的参加。
public class DatabaseCursorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HashMap<Integer, String> _dummy = new HashMap<Integer, String>();
OpenDatabaseHelper helper = new OpenDatabaseHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(OpenDatabaseHelper.GENRES_ID_KEY, 1);
values.put(OpenDatabaseHelper.GENRES_TITLE_KEY, "Test");
db.insert(OpenDatabaseHelper.GENRES_TABLE_NAME, null, values);
db.close();
helper.close();
db = helper.getReadableDatabase();
Cursor genresCursor = db.query(OpenDatabaseHelper.GENRES_TABLE_NAME, new String[]{OpenDatabaseHelper.GENRES_ID_KEY, OpenDatabaseHelper.GENRES_TITLE_KEY }, null, null, null, null, null);
int i = genresCursor.getColumnCount();
genresCursor.moveToFirst();
}
public class OpenDatabaseHelper extends SQLiteOpenHelper {
public static final String GENRES_TABLE_NAME = "genres";
public static final String GENRES_ID_KEY = "id";
public static final String GENRES_TITLE_KEY = "title";
public OpenDatabaseHelper(Context context) {
super(context, "ttt.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + GENRES_TABLE_NAME + "( id integer primary key not null, title text);" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}