마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

1개 adapter 에서 2개 이상 table 사용 방법 좀 알려주시면 감사하겠습니다.

0 추천

제목처럼 하는 이유는 drawerLayout에서 서랍장에 항목1 항목2 에 따라서 사용하는 table이 다르기 때문입니다.

그런데 현재는 table을 2개 만들어서 설정해놨음에도 항목1과 항목2가 분리되지 않고, 동일하게 insert/update/delete가 되고 있습니다.

여러 방법으로 테스트해봤으나, 방법을 모르겠어서 adapter 부분 소스가 잘못된건지 고수님들이 확인 좀 해주셨으면 해서 질문드립니다.

부탁드리겠습니다...

감사합니다!

public class NotesDbAdapter {
	public static final String KEY_TITLE = "title";
	public static final String KEY_BODY = "body";
	public static final String KEY_ROWID = "_id";
	private static final String TAG = "NotesDbAdapter";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;
	private static final String DATABASE_NAME = "data";
	private static final int DATABASE_VERSION = 3;
	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("create table notes (_id integer primary key autoincrement, "
					+ "title text not null, body text not null);");
			db.execSQL("create table notes1 (_id integer primary key autoincrement, "
					+ "title text not null, body text not null);");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS notes");
			db.execSQL("DROP TABLE IF EXISTS notes1");
			onCreate(db);
		}
	}

	public NotesDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public NotesDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long createNote(String title, String body) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_TITLE, title);
		initialValues.put(KEY_BODY, body);

		return mDb.insert("notes", null, initialValues);
	}

	public long createNote1(String title, String body) {
		ContentValues initialValues1 = new ContentValues();
		initialValues1.put(KEY_TITLE, title);
		initialValues1.put(KEY_BODY, body);

		return mDb.insert("notes1", null, initialValues1);
	}

	public boolean deleteNote(long rowId) {
		return mDb.delete("notes", KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean deleteNote1(long rowId) {
		return mDb.delete("notes1", KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor fetchAllNotes() {
		return mDb.query("notes",
				new String[] { KEY_ROWID, KEY_TITLE, KEY_BODY }, null, null,
				null, null, null);
	}

	public Cursor fetchAllNotes1() {
		return mDb.query("notes1", new String[] { KEY_ROWID, KEY_TITLE,
				KEY_BODY }, null, null, null, null, null);
	}

	public Cursor fetchNote(long rowId) throws SQLException {
		Cursor mCursor =
		mDb.query(true, "notes",
				new String[] { KEY_ROWID, KEY_TITLE, KEY_BODY }, KEY_ROWID
						+ "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor fetchNote1(long rowId) throws SQLException {
		Cursor mCursor =
		mDb.query(true, "notes1",
				new String[] { KEY_ROWID, KEY_TITLE, KEY_BODY }, KEY_ROWID
						+ "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public boolean updateNote(long rowId, String title, String body) {
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, title);
		args.put(KEY_BODY, body);
		return mDb.update("notes", args, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public boolean updateNote1(long rowId, String title, String body) {
		ContentValues args1 = new ContentValues();
		args1.put(KEY_TITLE, title);
		args1.put(KEY_BODY, body);
		return mDb.update("notes1", args1, KEY_ROWID + "=" + rowId, null) > 0;
	}
}

 

사자87 (240 포인트) 님이 2014년 12월 1일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...