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.
감사하지만 도움이되었지만 동기화 된 메서드는 항상 예외를 throw하여 포기하거나 액세스하기 전에 잠금을 해제해야합니까? – Muhammad
예외를 추가 할 수 있습니까? –
W/SQLiteConnectionPool (11141) : '/data/data/com.nilecode.matabat/databases/localobstaclesdb'데이터베이스의 SQLiteConnection 개체가 누수되었습니다. 진행중인 트랜잭션을 올바르게 끝내고 더 이상 필요없는 데이터베이스를 닫으려면 애플리케이션을 수정하십시오. – Muhammad