2014-01-11 3 views
0
public class LocalCached extends SQLiteOpenHelper{ 
     public boolean insertNewDataSet(ArrayList<Obstacle> newObs) { 

     if (newObs.size() > 0) { 
      db = this.getWritableDatabase(); 
      getRowsCount(); 
      switch (checkFlag) { 
      case NO_RECORDS: 

//    this.onCreate(db); 
//    for (int i = 0; i < newObs.size(); i++) { 
//     insertNewObstacle(newObs.get(i)); 
//    } 
//    break; 
      case THERE_IS_RECORDS: 
       checkDublicate(newObs.get(0), newObs.get(newObs.size() - 1)); 
       break; 
      case DATA_MATCHED: 
       if (MatabbatManager.DEBUG) 
        Log.i(MatabbatManager.TAG, "Data Already Exist"); 
       break; 
      case DATA_NOT_MATCHED: 
       // db = this.getWritableDatabase(); 
       for (int i = 0; i < newObs.size(); i++) { 
        insertNewObstacle(newObs.get(i)); 
       } 
       break; 
      default: 
       break; 
      } 
      db.close(); 
     } 
     return true; 
    } 

    public void getRowsCount() { 
     dblocs = this.getReadableDatabase(); 
     dblocs.rawQuery("SELECT * FROM " + OBSTACLES_TABLE, new String[] {}); 
     if (cur.getCount() != 0) { 
      checkFlag = THERE_IS_RECORDS; 
     } else { 
      checkFlag = NO_RECORDS; 
     } 
     cur.close(); 
     dblocs.close(); 
    } 

    public void checkDublicate(Obstacle firstObstacle, Obstacle lastObstacle) { 
     dblocs = this.getReadableDatabase(); 
     dblocs.rawQuery("SELECT * FROM " + OBSTACLES_TABLE, new String[] {}); 
     cur.moveToFirst(); 
     if (cur.getDouble(0) == firstObstacle.getLongitude() 
       && cur.getDouble(1) == firstObstacle.getLatitude()) { 
      cur.moveToLast(); 
      if (cur.getDouble(0) == lastObstacle.getLongitude() 
        && cur.getDouble(1) == lastObstacle.getLatitude()) { 
       checkFlag = DATA_MATCHED; 
      } 
     } else { 

      checkFlag = DATA_NOT_MATCHED; 

     } 

     cur.close(); 
     dblocs.close(); 
    } 

    public void insertNewObstacle(Obstacle newObstacle) { 

//  db = this.getWritableDatabase(); 
//   
     db.execSQL("Insert into " + OBSTACLES_TABLE + " Values (' " 
       + newObstacle.getLongitude() + "' , ' " 
       + newObstacle.getLatitude() + "' , ' " 
       + newObstacle.getDirection() + "' , ' " + newObstacle.getType() 
       + "' , ' " + newObstacle.getAddress() + "' , '" 
       + newObstacle.getSubmissionTime() + "' , '" 
       + newObstacle.getSubmitterName() + "')"); 
       db.close(); 
    } 

질문은 데이터베이스를 읽고 쓰는 방법입니까 아니면 불가능합니까?SqlLite 중복 레코드 확인

오류는 중복 레코드 (데이터 배열이므로 첫 번째 레코드와 마지막 레코드 만 확인)를 확인하려고 할 때 새 레코드가 열리기 전에 연결을 닫으려는 오류라고 말합니다.

업데이트 :

예외 :

A SQLiteConnection object for database '/data/data/com.nilecode.matabat/databases/localobstaclesdb' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 

답변

0

질문은 데이터베이스를 읽고 쓰는 방법입니까 아니면 불가능합니까?

getWritableDatabase()에 의해 반환 된 데이터베이스에서 읽을 수도 있습니다. documentation에서 :

읽기 및 쓰기에 사용될 데이터베이스를 만들거나 엽니 다.

0

당신이 난 단지 추측 할 수 compileable/전체 코드를 게시 didnt가있다. 그러나 이것은 잘 작동해야합니다.

public class LocalCached extends SQLiteOpenHelper { 

    SqlDataBase db = this.getReadableDatabase(); //Method 1 
    Cursor cursor = cursor.rawQuery("",null); 
    // check duplicate 
    cursor.close(); 
    db.close(); 

    db = this.getWritableeDatabase(); //Method 2 
    // do whatever 
    db.close(); 
} 

당신은 별도의 방법으로 확인을 할 수 있지만 방법 2 getWriteableDatabase 호출하기 전에 방법 1 호출되어 있는지 확인해야합니다.

그리고 가장 똑똑한 것처럼 보이는 백그라운드에서 db 작업을 수행하는 경우 앱을 정지시키지 않으므로 멀티 스레드 문제를 피하기 위해 메서드를 동기화해야 할 수 있습니다.

+0

감사하지만 도움이되었지만 동기화 된 메서드는 항상 예외를 throw하여 포기하거나 액세스하기 전에 잠금을 해제해야합니까? – Muhammad

+0

예외를 추가 할 수 있습니까? –

+0

W/SQLiteConnectionPool (11141) : '/data/data/com.nilecode.matabat/databases/localobstaclesdb'데이터베이스의 SQLiteConnection 개체가 누수되었습니다. 진행중인 트랜잭션을 올바르게 끝내고 더 이상 필요없는 데이터베이스를 닫으려면 애플리케이션을 수정하십시오. – Muhammad