public void insert(Integer recordid, String dealername, Integer dealamount, String strCurDate) {
String sql1 = "SELECT 1 FROM " + DBhelper.table_dealer + " WHERE " + DBhelper.RecordID +" = " + recordid;
Cursor cursor1 = database.rawQuery(sql1, null);
if (cursor1 !=null) {
cursor1.moveToFirst();
}
database.execSQL("INSERT INTO " + DBhelper.table_dealer + "(" + DBhelper.RecordID + "," + DBhelper.Name + ") SELECT " + recordid + ", '" + dealername + "'");
database.execSQL("INSERT INTO " + DBhelper.table_event + "(" + DBhelper.Date + ", " + DBhelper.Category + ") " +
"SELECT '" + strCurDate + "', 0");
//이전 SQL문에서 입력된 recordid 값을 찾아서 Event 테이블에 넣어줄 변수로 선언
String sql2 = "SELECT " + DBhelper.DealerID + " FROM " + DBhelper.table_dealer + " WHERE " + DBhelper.RecordID + " = " + recordid;
Cursor cursor2 = database.rawQuery(sql2, null);
if (cursor2 != null) {
cursor2.moveToFirst();
}
final int tmprecordid = recordid;
//Event 테이블의 ID 최대값을 찾아내 tmpeventid로 선언
String sql3 = "SELECT MAX(" + DBhelper.EventID + ") AS EventID FROM " + DBhelper.table_event;
Cursor cursor3 = database.rawQuery(sql3, null);
if (cursor3 == null) {
cursor3.moveToFirst();
}
int tmpeventid = cursor3.getInt(cursor3.getColumnIndex("EventID"));
database.execSQL("INSERT INTO " + DBhelper.table_deal + "(" + DBhelper.DealerID + ", " + DBhelper.Amount + ", " + DBhelper.EventID + ") " +
"SELECT " + tmprecordid + ", " + dealamount + ", " + tmpeventid);
}
DB 파일 내에 dealer, deal, event라는 세 개의 테이블을 생성하였습니다.
현재 각 테이블의 구문을 각각 contentsValues 를 이용해 넣는 것 보다는 String으로 SQL문을 선언해서 사용하고 있는데요.
마지막 deal테이블 입력 구문에서 오류가 발생합니다.(오류 내용은 CursorIndexOutOfBoundsException입니다..)
커서를 통해 event테이블의 eventID라는 필드에서 최대값을 찾아내서 deal테이블의 같은 필드에 넣고 싶습니다.
실력이 일천해서 오류에 대한 대응을 못하고 있습니다 ㅠㅠ 도와주십시오...
하루종일 싸매고 있었는데도 진전이 없으니 미치겠네요 ㅠㅠ