SQLite检查重复记录

时间:2014-01-11 18:58:14

标签: android sqlite duplicates

    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();
    }

问题是如何获取读写数据库,或者这是不可能的?

错误说要在打开新的连接之前关闭连接,当我试图检查重复记录时(这是一个数据数组,所以我只检查第一个和最后一个记录)。

更新

例外:

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.

2 个答案:

答案 0 :(得分:0)

由于你没有发布完整/可编译的代码,我只能猜测。但这应该工作得很好。

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。

如果你在后台进行数据库工作,这似乎是最聪明的事情,因为你不希望你的应用挂起,你可能想让你的方法同步以避免多线程问题。

答案 1 :(得分:0)

  

问题是如何获取读写数据库,或者这是不可能的?

您还可以对getWritableDatabase()返回的数据库执行读取操作。来自documentation

  

创建和/或打开将用于读写的数据库。