DatabaseObjectNotClosedException in view pager
===

I have a custom horizontal view pager to page through list views each of which is populated from entries in a database tables. There will be as much list view as there are tables in the database. Now the problem is that I get DatabaseObjectNotClosedException. I have three relevant methods in the DbHelper.  Here's the code in my DbHelper.

    public int countTables() {
		int count = 0;
		String SQL_GET_ALL_TABLES = "SELECT * FROM sqlite_master WHERE type='table'";
		Cursor cursor =DB.rawQuery(SQL_GET_ALL_TABLES, null);
		cursor.moveToFirst();
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			count = cursor.getCount();
		}
		cursor.close();
		count = count - 2;
		return count;
	}

	public String getTableName(int count) {
		String tabname = "";
		String SQL_GET_ALL_TABLES = "SELECT * FROM SQLITE_SEQUENCE";
		Cursor cursor = DB.rawQuery(SQL_GET_ALL_TABLES, null);
		cursor.moveToPosition(count);
		tabname = cursor.getString(cursor.getColumnIndex("name"));
		cursor.close();
		DB.close();
		return tabname;
	}
    public Cursor getAll(String tableName) {
		return (getReadableDatabase().rawQuery("SELECT _id, note type FROM "
				+ tableName, null));

	}

Here's my main activity containing the view pager class.

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewp);
		MyPagerAdapter adapter = new MyPagerAdapter();
		ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);
		myPager.setAdapter(adapter);
		DbHelper helper = new DbHelper(this);
		count = helper.countTables();
		myPager.setCurrentItem(count - 1);
		temp1 = temp = helper.countTables() - 1;
    private class MyPagerAdapter extends PagerAdapter {

		public int getCount() {
			return count;
		}

		public Object instantiateItem(View collection, int position) {

			LayoutInflater inflater = (LayoutInflater) collection.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			helper = new DbHelper(Listing.this);
			View view = null;

			if (position == count) {
				view = inflater.inflate(R.layout.middle, null);
				ListView list = (ListView) view.findViewById(R.id.listView1);
				// helper = new DbHelper(Listing.this);
				String tabName = helper.getTableName(position);
				dataset_cursor = helper.getAll(tabName);
				startManagingCursor(dataset_cursor);
				adapter5 = new NoteAdapter(dataset_cursor);
				list.setAdapter(adapter5);
				// adapter.notifyDataSetChanged();
				temp--;
				temp1--;
			}

			if (position < temp) {
				temp--;
				temp1--;
				view = inflater.inflate(R.layout.left, null);
				ListView list1 = (ListView) view.findViewById(R.id.listView1);
				// helper = new DbHelper(Listing.this);
				String tabName = helper.getTableName(position);
				dataset_cursor1 = helper.getAll(tabName);
				startManagingCursor(dataset_cursor1);
				adapter1 = new NoteAdapter(dataset_cursor1);
				list1.setAdapter(adapter1);
				// adapter1.notifyDataSetChanged();
				// Toast.makeText(getBaseContext(), tabName, Toast.LENGTH_LONG);
				temp--;
				temp1--;
			}
			if (position > temp) {
				temp++;
				temp1++;
				view = inflater.inflate(R.layout.right, null);
				ListView list2 = (ListView) view.findViewById(R.id.listView1);
				// helper = new DbHelper(Listing.this);
				String tabName = helper.getTableName(position);
				dataset_cursor2 = helper.getAll(tabName);
				startManagingCursor(dataset_cursor2);
				adapter2 = new NoteAdapter(dataset_cursor2);
				list2.setAdapter(adapter2);
				// adapter2.notifyDataSetChanged();
			}

			((ViewPager) collection).addView(view, 0);
			return view;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);

		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == ((View) arg1);

		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

	}
countTable() is first executed and getTableName() is second.
I cannot use DB.close() in the method countTables because while I fetch the table names using getTableName the activity force closes saying database already closed.

I tried overriding onDestroy, onStop etc. I think its of no use because the Execption shows up when I am spaging through the listViews.

How can I get rid of this DatabaseObjectNotClosedException ?