我正在尝试在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();
}
答案 0 :(得分:0)
使用您的交易,如:
db.beginTransaction();
try{
//Process inserts
//....
db.setTransactionSuccessful();
}catch(Exception e){
}finally{
db.endTransaction();
}
答案 1 :(得分:0)
在Android中使用数据库事务的正确方法:
beginTransaction()
- 开始交易setTransactionSuccessful()
- 提交事务,它将提交数据库中的值endTransaction()
- 结束数据库事务。注意:如果您开始交易,则需要关闭交易。
以下是交易的标准惯用语:
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();
}