2014-01-11 55 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

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

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

你可以在一个单独的方法检查,但你必须确保方法1日前方法2调用getWriteableDatabase被调用。

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

+0

感谢有帮助的人,但同步方法总是抛出一个异常,所以我放弃它,或者我需要在访问它之前解锁? – Muhammad

+0

你能添加异常吗? –

+0

W/SQLiteConnectionPool(11141):数据库'/data/data/com.nilecode.matabat/databases/localobstaclesdb'的SQLiteConnection对象泄漏了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。 – Muhammad