sqlite事务已锁定

时间:2014-02-23 08:48:31

标签: java android sql sqlite transactions

我正在尝试在sqllite数据库中使用beginTransaction()来读取excel文件,但是我遇到了一些问题,获取错误数据库被锁定

这是我的编辑。 请注意我使用此代码五次插入5个excel文件

代码:

        dbHandler.open();
    try {
        url = new URL(
                "http://ictfox.com/Hafil_Updates/ImportExcel/schoolmaster.csv");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                url.openStream(), "Windows-1256"));
        while ((line = in.readLine()) != null) {

            String[] insertValues = line.split(",");
            if (flag_is_header) {

                dbHandler = new MySQLiteHelper(context);

                long row = dbHandler.insertSchoolMaster(insertValues[0],
                        insertValues[1], insertValues[2], insertValues[3],
                        insertValues[4], insertValues[5]);
                Log.e("no. of rows inserted", "" + row);

            } else {
                flag_is_header = true;
            }
        }
  dbHandler.setsucss();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        dbHandler.end();
        dbHandler.close();
    }
    return line;
}

    public void open()
{

    sqliteDB=this.getWritableDatabase();
    sqliteDB.beginTransaction();

}

     public void setsucss() {


 sqliteDB.setTransactionSuccessful();

}


// Close Database 
public void end() {


 sqliteDB.endTransaction();

}   

2 个答案:

答案 0 :(得分:0)

使用您的交易,如:

db.beginTransaction(); 
try{
//Process inserts
//....
db.setTransactionSuccessful(); 
}catch(Exception e){

}finally{
db.endTransaction();
}

答案 1 :(得分:0)

在Android中使用数据库事务的正确方法:

  1. beginTransaction() - 开始交易
  2. setTransactionSuccessful() - 提交事务,它将提交数据库中的值
  3. endTransaction() - 结束数据库事务。
  4. 注意:如果您开始交易,则需要关闭交易。

    以下是交易的标准惯用语:

    db.beginTransaction();
       try {
         ...
         db.setTransactionSuccessful();
       } finally {
         db.endTransaction();
       }
    

    参考:SQLiteDatabse

    示例:

    mDatabase.beginTransaction();
    try{
      //do multiple database operations here
      //which throws exceptions on error
      mDatabase.setTransactionSuccessful();
      //do not any more database operations between
      //setTransactionSuccessful and endTransaction
      }catch(Exception e){
        //end the transaction on error too when doing exception handling
        mDatabase.endTransaction();
        throw e;
      }
      //end the transaction on no error
      mDatabase.endTransaction();
    }