当我在 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);
}
}
}
然后我尝试在活动中打开数据库:
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();
}
}
}
并且它会抛出一个错误的数据库。在单击按钮期间抛出。如何处理?