-2

when I create a SQLiteDatabase in App and insert into the table a data. It's throws a mistake show me that database is locked.

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);

    }

}

}

then I try to Open database in 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();
        }
    }

}

and it's throw a mistake database is looked.the throws during button onClicked. how to deal it?

Mryoun
  • 153
  • 1
  • 3
  • 10

1 Answers1

1

It's throws a mistake show me that database is locked.

Probably you forgot to close your database or this problem occures when another thread is accessed(and doing some work) to your database when you are trying to deal with it from actual thread.

Make sure that you are connected to database only from one thread at time(one connection at time). You cannot deal with database from different threads at same time.

Note: It's good practise an usage of synchronized blocks and methods if you are creating concurrent application.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • No there is only One MainThread that Activity. no anonther Thread. I just defend a class include inner class that extends SQLiteOpenHelper I find the throws happended in oncreate in SQLiteOpenHelper when it doing SQLiteDatabase.openOrCreateDatabase( "/data/data/com.example.ch_2013_3_19sqlite/databases/" + strDBName, null); – Mryoun Apr 07 '13 at 22:37
  • @Mryoun check fou don't have your db opened or add here full java code. – Simon Dorociak Apr 07 '13 at 22:45
  • would U like tell me you e-mail address? there limited word counts – Mryoun Apr 07 '13 at 22:57
  • @Mryoun edit your question and post relevant code - where you are dealing with database. – Simon Dorociak Apr 07 '13 at 23:09