public void insert(int citynum, String city, String country, int game, int win, int top, int top4) {
SQLiteDatabase db = getWritableDatabase();
String sql = "INSERT INTO STAT(citynum, city, country, game, win, top, top4)" + " VALUES (" + citynum + ", '" + city + "', '" + country + "', " + game + ", " + win + ", " + top + ", " + top4 + ");";
db.execSQL(sql);
db.close();
위가 데이터를 삽입하는 코드입니다. insert()메소드를 통해서 100개가량의데이터를 집어넣었습니다.
dbHelper = new DBHelper(this, dbName, null, dbVersion);
db = dbHelper.getWritableDatabase();
sql = "SELECT * FROM stat";
cursor = db.rawQuery(sql, null);
cursor.moveToPosition(10);
int test = cursor.getInt(7);
Toast.makeText(this, String.valueOf(test), Toast.LENGTH_LONG ).show();
이후 데이터 확인을 위해서 onCreate에서 위의 코드를 실행되게 했습니다. 그럼 7번재줄의 int가 토스트로 떠야하는데 "int test = cursor.getInt(7)에서 CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0가 뜹니다. 제 추측에는 데이터 삽입할때 뭔가 문제가 생겨서 테이블이 젣제대로 안생긴거 같은데, 어디가 문젠지 잘 모르겠어서 질문드립니다 ㅠㅠ
참고로 테이블 생성은 다음과 같습니다..
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE STAT (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "citynum INTEGER, city TEXT, country TEXT, game INTEGER, win INTEGER, top INTEGER, top4 INTEGER);");
}
또 insert메소드 사용은 다음과 같이 했습니다.
insert(1, "Seoul", "Korea", 0, 0, 0, 0);
insert(2, "Busan", "Korea", 0, 0, 0, 0);
insert(3, "Tokyo", "Japan", 0, 0, 0, 0); ... 반복