attempt to re-open an already-closed object //是因為可能要讀和寫資料庫 //解決方式:不要將Databse關閉,要關閉則透過生命週期去關閉 //有用到Cursor則將其關閉即可 Cannot perform this operation because there is no current transaction. //是因為有重複執行該方法 //例如 beginTransaction(); setTransactionSuccessful(); endTransaction(); endTransaction(); 解決方式:找出會重複執行的方法並刪去不需要的即可
2015/10/30
Android SQLite java.lang.IllegalStateException常見錯誤
2015/10/27
Android SQLite 讀/寫
參考
Code:
Code:
private void ReadDatabase() { //取得可讀取資料庫 SQLiteDatabase db = new Database(context).getReadableDatabase(); //查詢資料庫 cursor = db.rawQuery("SELECT * FROM MyOptions", new String[] {}); //取得資料筆數 // cursor.getCount() //持續移動到下一筆資料 while (cursor.moveToNext()) { //取得Name欄位資料 cursor.getString(cursor.getColumnIndex("Name")); //取得Doc欄位資料 cursor.getString(cursor.getColumnIndex("Doc")); } //關閉指標 cursor.close(); //關閉資料庫 db.close(); } private void WriteDatabase() { //取得可寫入資料庫 SQLiteDatabase db = new Database(context).getWritableDatabase(); //寫入資料 ContentValues values = new ContentValues(); //欄位, 值 values.put("Name", "values")); //-1則是新增失敗 if (db.insert("MyOptions", null, values) != -1) Toast.makeText(AddOptionActivity.this, "新增成功", 1).show(); else Toast.makeText(AddOptionActivity.this, "新增失敗", 1).show(); //關閉資料庫 db.close(); }
2014/02/26
Android建立SQLite並新增查詢
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/buttonSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Save" /> <Button android:id="@+id/buttonLoad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/buttonSave" android:layout_alignBottom="@+id/buttonSave" android:layout_centerHorizontal="true" android:text="Load" /> </RelativeLayout>
訂閱:
文章 (Atom)