简体   繁体   English

多次写入android sqlite3

[英]Multiple writes for android sqlite3

How can we write simultaneously multiple time in sqlite3 database in android if I am performing a database operation from an activity and at same time if my android service starts (Which also perform database insertion) then how to handler the database operation. 如果我要通过活动执行数据库操作,而我的android服务同时启动(还要执行数据库插入),那么如何在android的sqlite3数据库中同时写入多个时间,然后如何处理数据库操作。

as android developer website says http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#setLockingEnabled%28boolean%29 就像android开发人员网站所说的http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#setLockingEnabled%28boolean%29

that SetLocakingEnabled " method is deprecated ". SetLocakingEnabled“ 方法已弃用 ”。 So how we can access multiple write(insertion) operation in Database. 因此,我们如何访问数据库中的多个write(insertion)操作。

I also referred to the documentation of sqlite for locking http://www.sqlite.org/faq.html#q5 我还参考了sqlite的文档来锁定http://www.sqlite.org/faq.html#q5

please provide any example for the same . 请提供相同的任何例子。

Thanks 谢谢

I'm not sure what you mean by multiple write access. 我不确定多重写入访问的含义。 But what you probably want to do is lock the database with a transaction and perform your inserts, then when complete end the transaction. 但是您可能想做的是用事务锁定数据库并执行插入操作,然后在完成事务后结束操作。

//in your database access class...assuming you have a list of objects to insert

public function void multiple_write(ArrayList<Object> list) {
  //this locks the database
  db.beginTransaction();
  for (Object obj : list) {
    db.insert(TABLE_NAME, null, getContentValues(obj));
  }

  db.setTransactionSuccessful();
  //this completes your transaction
  db.endTransaction();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM