最近开发中,需要再多线程中操作数据库,但是Android的sqlite数据库是不能多线程写读写的。
先看一下报的错误:
android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
看一下代码
public class DBTestActivity extends BaseActivity {
private Button mAddStudent_bt;
@Override
protected void initView() {
setContentView(R.layout.ac_dbtest);
mAddStudent_bt=(Button)findViewById(R.id.ac_add_student_bt);
mAddStudent_bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addStudent();
}
});
}
private void addStudent() {
//模拟1000个线程 加入数据库
for(int i=0;i<1000;i++){