0
11-01 18:20:09.953: E/AndroidRuntime(528): FATAL EXCEPTION: main
11-01 18:20:09.953: E/AndroidRuntime(528): java.lang.IllegalStateException: database not open
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1526)
11-01 18:20:09.953: E/AndroidRuntime(528):  at phoenix.metro.uis.DBHelper.insertCart(DBHelper.java:149)
11-01 18:20:09.953: E/AndroidRuntime(528):  at phoenix.metro.uis.SearchActivity$4.onClick(SearchActivity.java:171)
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.view.View.performClick(View.java:2485)
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.view.View$PerformClick.run(View.java:9080)
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.os.Handler.handleCallback(Handler.java:587)
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.os.Looper.loop(Looper.java:123)
11-01 18:20:09.953: E/AndroidRuntime(528):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-01 18:20:09.953: E/AndroidRuntime(528):  at java.lang.reflect.Method.invokeNative(Native Method)
11-01 18:20:09.953: E/AndroidRuntime(528):  at java.lang.reflect.Method.invoke(Method.java:507)
11-01 18:20:09.953: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-01 18:20:09.953: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-01 18:20:09.953: E/AndroidRuntime(528):  at dalvik.system.NativeStart.main(Native Method)

The database code as below :

public class DBHelper extends SQLiteOpenHelper{

    //SQLiteOpenHelper : A helper class to manage database creation and version management. 
    private final static String sdb_NAME="metro_sdb";
    private static int sdbVersion = 2; // version must be >=1
    //Table name ---------------------------------
    private final static String TABLE_NAME="Item";
    private final static String TABLE_NAME_CART="Cart";


    //Table columns (cart) ------------------------------
    private final static String COL_CART_ID="id";
    private final static String COL_CART_ADDDATE="name";
    public DBHelper(Context context) {
        super(context, sdb_NAME, null, sdbVersion); // null is default for cursor factory
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        //Called when the database is created for the first time.

        String sql2 = "CREATE TABLE " + TABLE_NAME_CART +"("+
                COL_CART_ID +" TEXT,"+
                COL_CART_ADDDATE + " TEXT NOT NULL);";

        db.execSQL(sql2);
    }
    @Override
    public void onUpgrade(SQLiteDatabase  db, int oldVersion, int newVersion) {
        //Called when the database needs to be upgraded.
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        String sql2 = "DROP TABLE IF EXISTS " + TABLE_NAME_CART;
         db.execSQL(sql);
         db.execSQL(sql2);
        onCreate( db);
    }

    public boolean insertCart(Cart cart){
        SQLiteDatabase sdb = getWritableDatabase();

        if(getCartItems(cart.id).size()>0){

            ContentValues values = new ContentValues();

            values.put(COL_ITEM_ID, cart.id);
            values.put(COL_CART_ADDDATE, cart.addDate);
            //long re = sdb.insert(TABLE_NAME_CART, null, values);

            long re = sdb.insertWithOnConflict(TABLE_NAME_CART, null, values,SQLiteDatabase.CONFLICT_REPLACE);
            sdb.close();

            return re > 0;
        }else{
            sdb.close();
            return false;
        }
    }

The error is inside the database operations in insertCart() function

4

2 回答 2

1

你可以尝试删除sdb.close()并再次检查它。

于 2012-11-01T19:03:46.817 回答
0

此错误的可能原因之一是您正在打开已经打开的数据库。

 if (!sdb.isOpen()) {
   System.out.println("If executed");
   sdb = getApplicationContext().openOrCreateDatabase(DATABASE_PATH, SQLiteDatabase.OPEN_READWRITE, null);
 } else {
   System.out.println("Else executed");
 }

尝试这个..,。

于 2012-11-02T05:13:00.133 回答