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.
感谢有帮助的人,但同步方法总是抛出一个异常,所以我放弃它,或者我需要在访问它之前解锁? – Muhammad
你能添加异常吗? –
W/SQLiteConnectionPool(11141):数据库'/data/data/com.nilecode.matabat/databases/localobstaclesdb'的SQLiteConnection对象泄漏了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。 – Muhammad