Note that there are some explanatory texts on larger screens.

plurals
  1. POabout database is locked
    text
    copied!<p>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.</p> <pre><code>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) &amp;&amp; (!cursor.moveToFirst())) { // return null; } TblUser[] tbluser = new TblUser[RecordCount]; for (int i = 0; i &lt; 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); } } </code></pre> <p>}</p> <p>then I try to Open database in Activity:</p> <pre><code> 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(); } } } </code></pre> <p>and it's throw a mistake database is looked.the throws during button onClicked. how to deal it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload