关于数据库被锁定

时间:2013-04-07 21:38:46

标签: android database

当我在App中创建SQLiteDatabase并在表中插入数据时。这是一个错误,告诉我数据库已被锁定。

public class DButils {

//
private DBopenHelper mySqliteDBHelper;
private Context myContext;
static SQLiteDatabase mySqliteDatabase;
//
private static String strDBName = "kar.db";
private static int version = 1;
//
private static String strTblName = "people";
private static String strId = "id";
private static String strUser = "name";
private static String strSalary = "salary";
private static String strRecorddate = "date";

private static String strSql = "create table " + strTblName + " (" + strId
        + " integer primary key , " + strUser + " text not null, "
        + strSalary + " integer," + strRecorddate + " text);";

public DButils(Context myContext) {
    this.myContext = myContext;
    mySqliteDBHelper = new DBopenHelper(myContext, strDBName, null, version);
}

// Open
public void OpenDB() {
    // 
    if (mySqliteDBHelper != null)
        mySqliteDatabase = mySqliteDBHelper.getWritableDatabase();
}

/**
 * insert
 * 
 * @param user
 * @return
 */
public long inSert(TblUser user) {
    ContentValues convalues = new ContentValues();
    convalues.put(strId, user.getId());
    convalues.put(strUser, user.getName());
    convalues.put(strSalary, user.getSalary());
    convalues.put(strRecorddate, user.getRecordDate());
    return mySqliteDatabase.insert(strTblName, null, convalues);
}

/**
 * select
 */
public TblUser[] selectAll() {
    Cursor cursor = mySqliteDatabase.query(strTblName, new String[] {
            strId, strUser, strSalary, strRecorddate }, null, null, null,
            null, null);

    return convert(cursor);
}

/**
 * select a data
 */

public TblUser[] selectOne(int id) {
    Cursor cursor = mySqliteDatabase.query(strTblName, new String[] {
            strId, strUser, strSalary, strRecorddate }, "id" + id, null,
            null, null, null);
    return convert(cursor);
}

/**
 * change
 */
public TblUser[] convert(Cursor cursor) {
    // 
    int RecordCount = cursor.getCount();
    // 
    if ((RecordCount == 0) && (!cursor.moveToFirst())) {
        //
        return null;
    }

    TblUser[] tbluser = new TblUser[RecordCount];

    for (int i = 0; i < RecordCount; i++) {
        cursor.moveToNext();
        tbluser[i] = new TblUser();
        tbluser[i].setId(cursor.getInt(0));
        tbluser[i].setName(cursor.getString(1));
        tbluser[i].setSalary(cursor.getInt(2));
        tbluser[i].setRecordDate(cursor.getString(3));
    }
    return tbluser;

}

// close
public void DBclose() {
    if (mySqliteDatabase != null) {
        mySqliteDatabase.close();
        mySqliteDatabase = null;
    }
}

// helper
public class DBopenHelper extends SQLiteOpenHelper {

    public DBopenHelper(Context context, String name,
            CursorFactory factory, int version) {

        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
        System.out.println("doing construct");
    }

    @SuppressLint("SdCardPath")
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        System.out.println("doing oncreate");
        SQLiteDatabase.openOrCreateDatabase(
                "/data/data/com.example.ch_2013_3_19sqlite/databases/"
                        + strDBName, null);
        db.execSQL(strSql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("drop table if exists " + strDBName);

    }

}

}

然后我尝试在Activity中打开数据库:

 DButils db;
//btn
private Button btnSave;
private Button btnRead;

//textView
private TextView textView;
btnOnclick mybtnonclick;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mybtnonclick=new btnOnclick();
    btnSave=(Button)findViewById(R.id.button1);
    btnRead=(Button)findViewById(R.id.button2);
    btnSave.setOnClickListener(mybtnonclick);
    btnRead.setOnClickListener(mybtnonclick);

    textView=(TextView)findViewById(R.id.textView2);


    db=new DButils(this);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class btnOnclick implements OnClickListener{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==btnSave){

            db.OpenDB();
            TblUser user=new TblUser();
            user.setId(10);
            user.setName("mario");
            user.setSalary(100);
            user.setRecordDate("2013-2-22");
            db.inSert(user);
            db.DBclose();

        }
        if(v==btnRead){
            db.OpenDB();
            TblUser[] tbl=db.selectAll();
            String str="name: "+tbl[0].getName()+" id:"+
            tbl[0].getId()+" salary:"+tbl[0].getSalary()+
            " date :"+tbl[0].getRecordDate();

            textView.setText(str);
            db.DBclose();
        }
    }

}

并且抛出一个错误的数据库被查看。按钮onClicked时抛出。怎么处理?

1 个答案:

答案 0 :(得分:1)

  

错误地告诉我数据库已被锁定。

当您尝试从实际线程处理另一个线程时,您可能忘记关闭数据库,或者当您正在尝试从数据库访问另一个线程(并执行某些工作)时,会出现此问题。

确保您仅在一个线程时连接到数据库(一次连接一次)。你不能同时处理来自不同线程的数据库。

注意:如果要创建并发应用程序,最好使用synchronized块和方法。