2012-01-19 2 views
0

단일 행 (빈 테이블)이없는 테이블에 값을 삽입해야합니다. 이미 데이터가 테이블에 있으면 삽입 할 필요가 없습니다. 다음 코드를 사용했습니다.테이블이 비어 있으면 테이블에 값을 삽입하는 데 도움이 필요합니다.

sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null); 
     sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + SAMPLE_TABLE_NAME 
       + " (rowid INT , songname VARCHAR," + " count INT);"); 
     int len = MP3Activity.songlist.size(); 
     Log.v("songlist len", Integer.toString(len));   
     cur = sampleDB.rawQuery("SELECT COUNT(*) FROM "+ SAMPLE_TABLE_NAME ,null); 
     if (cur != null) { 
       if(cur.getInt(0)<=1) //exception at this line. 
        { 
         for (int s = 0; s < MP3Activity.songlist.size(); s++) { 
          Log.v("Before insert", "1"); 
           sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (" 
             + row_id++ + ",\"" + MP3Activity.songlist.get(s) + "\"," 
             + count + ");");   
           Log.v("After insert", "1"); 
          } 
        } 
        else 
        { 
         Log.v("inside else......",""); 
        } 

      }   

     cur.close();   

     c = sampleDB.rawQuery("SELECT songname, count, rowid FROM " 
       + SAMPLE_TABLE_NAME + " where count=0 ", null); 

     if (c != null) { 
      if (c.moveToFirst()) { 
       do { 
        String songName = c.getString(c.getColumnIndex("songname")); 
        int count = c.getInt(c.getColumnIndex("count")); 
        int rid=c.getInt(c.getColumnIndex("rowid")); 
        results.add("Rowid: " + rid + ",Song Name: " + songName + ",Count: " + count); 
       } while (c.moveToNext()); 
      } 
     } 
     c.close(); 
     int reslen = results.size(); 
     Log.v("Length of result", Integer.toString(reslen)); 

     for (int i = 0; i < results.size(); i++) { 
      Log.v("Element in result", results.get(i)); 
     } 
     /*for (int k = 0; k < results.size(); k++) { 

      if (sampleDB != null) 
       sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME); 

     }*/ 
     sampleDB.close(); 

다음과 같은 예외가 있습니다. 열 값에 커서를 액세스하기 전에 즉 c.moveToFirst의 결과를 확인() :

01-19 15:22:42.059: E/AndroidRuntime(464): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 
01-19 15:22:42.059: E/AndroidRuntime(464): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
01-19 15:22:42.059: E/AndroidRuntime(464): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172) 
01-19 15:22:42.059: E/AndroidRuntime(464): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84) 
01-19 15:22:42.059: E/AndroidRuntime(464): at com.mp3.Frequent.onCreate(Frequent.java:46) 
01-19 15:22:42.059: E/AndroidRuntime(464): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
01-19 15:22:42.059: E/AndroidRuntime(464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 

답변

0

당신은 첫 번째 요소 (있는 경우) 당신이 두 번째 쿼리처럼 커서를 이동해야합니다.

+0

고마워, 이미 시도해 봤어. 그런 식으로 사용하면 두 번째로 (테이블도 비어 있지 않음) 데이터가 삽입됩니다. – Manikandan

0

테이블이 비어있는 경우 다음이 줄을 열 인덱스 0을 얻을 수있는 방법을

if(cur.getInt(0)<=1) //exception at this line. 

그렇게

if(cur.getCount()<=1) //exception at this line. 

편집하여 대체 :
대신에 당신이 할 수있는 조건 위에 체크 이것도 확인해주세요

if(cur.getCount()>0) //exception at this line. 
     { 
//Already Inserted value 
    } 
     else 
      { 
        for (int s = 0; s < MP3Activity.songlist.size(); s++) { 
         Log.v("Before insert", "1"); 
          sampleDB.execSQL("INSERT INTO " + SAMPLE_TABLE_NAME + " Values (" 
            + row_id++ + ",\"" + MP3Activity.songlist.get(s) + "\"," 
            + count + ");");   
          Log.v("After insert", "1"); 
         } 

       } 
+0

감사합니다. 나는 두 번째로 (심지어 테이블이 공백이 아님) 데이터를 삽입했다. – Manikandan

+0

조건이 true 일 때 다시 "<= 1" –

+0

CursorIndexOutOfBoundsException으로 인해 오류가 발생하여 부분을 편집하십시오. 그리고 인덱스 -1 및 cur.getInt (0)이 인덱스 0의 행 값을 가져 오는 경우 –

관련 문제