-2

I am using SQLite Database to get the product name, quantity, price but when ever i press add_to_cart button, it force closes.

This is what my debugger says, how to debug this

public void addToCart(Order order){
   SQLiteDatabase db = getReadableDatabase();
   String query = String.format("INSERT INTO OrderDetail(Productid,ProductName,Quantity,Price) VALUES('%s','%s','%s','%s');",
    order.getProductid(),
    order.getProductName(),
    order.getQuantity(),
    order.getPrice());
    db.execSQL(query);
}

btncart = (FloatingActionButton)findViewById(R.id.btncart);
btncart.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       //line 49// 
        new Database(FoodDetails.this).addToCart(new Order(
            foodId,
            currentFood.getName(),
            numberButton.getNumber(),
            currentFood.getPrice()
        ));
        Toast.makeText(FoodDetails.this,"Added to cart",Toast.LENGTH_SHORT).show();
    }
});

This is the given StackTrace:

at com.k.menu.Database.Database.addToCart(Database.java:54) at com.k.menu.FoodDetails$1.onClick(FoodDetails.java:49) These two are the errors i got from logcat

1-19 19:43:14.767 9304-9304/com.k.menu E/AndroidRuntime: FATAL EXCEPTION: main
                                                      Process: com.k.menu, PID: 9304
                                                      com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/eatitDb.db file (or .zip, .gz archive) in assets, or target folder not writable
                                                          at android.content.res.AssetManager.openAsset(Native Method)
                                                          at android.content.res.AssetManager.open(AssetManager.java:347)
                                                          at android.content.res.AssetManager.open(AssetManager.java:321)
                                                          at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.copyDatabaseFromAssets(SQLiteAssetHelper.java:436)
                                                          at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:400)
                                                          at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
                                                          at com.k.menu.Database.Database.addToCart(Database.java:54)
                                                          at com.k.menu.FoodDetails$1.onClick(FoodDetails.java:49)
                                                          at android.view.View.performClick(View.java:5637)
                                                          at android.view.View$PerformClick.run(View.java:22429)
                                                          at android.os.Handler.handleCallback(Handler.java:751)
                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                          at android.os.Looper.loop(Looper.java:154)
                                                          at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

The below link has my application database and food details https://www.dropbox.com/sh/j2j4umz5wv2o7t4/AACIExIYT24SjiYlxbZ0Ut24a?dl=0

Mike M.
  • 38,532
  • 8
  • 99
  • 95
Karthik N
  • 23
  • 8

4 Answers4

3

SQLiteAssetException: Missing databases/eatitDb.db file (or .zip, .gz archive) in assets, or target folder not writable

Means exactly that either the file eatitDb.db is not in the assets/databases directory or for some reason the target folder is not writeable.

So 1) check that the file copied into the assets/databases file is named eatitDb.db. If it isn't the copy it into that location, clear the App's data or uninstall the App (probably not required) and then rerun the App.

2) If the file is located then ensure that it is copied to somewhere where it will be writeable (although if it weren't then you couldn't copy it) and that you aren't changing the file permsissions.


Note re getReadableDatabase

Changing getReadableDatabase to getWriteableDatabase will not resolve the problem. As getReadableDatabase will get a writeable database if it can: as per :-

Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.

getReadableDatabase

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • @KarthikN I'd appreciate you marking this as the correct answer rather than the other answer. – MikeT Jan 20 '18 at 19:08
0

try using getWritableDatabase instead of getReadableDatabase.

https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase()

user3761001
  • 118
  • 4
  • `getReadableDatabase` will get a writeable database unless it cannot. If you follow your link and look at `getReadableDatabase` it says :-*Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.* – MikeT Jan 20 '18 at 11:37
0

Check your application file structure and see if the database.db file is placed in the right directory.

Where to place the database file?

Place your db file in /src/main/assets/databases/your_db_file.db (this file must have extension of .db or .zip)

Araali Farooq
  • 662
  • 6
  • 4
0

kindly change folder name from database to databases

Karem Mohamed
  • 361
  • 4
  • 11