0

I created DB table from DB Brower for SQLite and save to the below path but getting error when I clicked the button to read db file. Not sure whether it is due to file location or my code.

enter image description here

But when I try to open the DB from the code it gives me below errors

2020-04-04 10:19:51.975 6995-6995/com.dav2020.orderforme W/SQLiteAssetHelper: copying database from assets... 2020-04-0

4 10:19:51.982 6995-6995/com.dav2020.orderforme E/SQLiteAssetHelper: Couldn't open OrderForMeDB for writing (will try read-only):
    com.readystatesoftware.sqliteasset.SQLiteAssetHelper$SQLiteAssetException: Missing databases/OrderForMeDB 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.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
        at com.dav2020.orderforme.Database.Database.addToCart(Database.java:50)
        at com.dav2020.orderforme.FoodDetail$1.onClick(FoodDetail.java:57)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        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:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
2020-04-04 10:19:51.982 6995-6995/com.dav2020.orderforme E/SQLiteLog: (14) cannot open file at line 32460 of [69906880ce]
2020-04-04 10:19:51.982 6995-6995/com.dav2020.orderforme E/SQLiteLog: (14) os_unix.c:32460: (2) open(/data/user/0/com.dav2020.orderforme/databases/OrderForMeDB) - 
2020-04-04 10:19:51.983 6995-6995/com.dav2020.orderforme E/SQLiteDatabase: Failed to open database '/data/user/0/com.dav2020.orderforme/databases/OrderForMeDB'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
        at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264)
        at com.dav2020.orderforme.Database.Database.addToCart(Database.java:50)
        at com.dav2020.orderforme.FoodDetail$1.onClick(FoodDetail.java:57)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        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:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
2020-04-04 10:19:51.983 6995-6995/com.dav2020.orderforme D/AndroidRuntime: Shutting down VM
2020-04-04 10:19:51.983 6995-6995/com.dav2020.orderforme E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dav2020.orderforme, PID: 6995
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
        at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264)
        at com.dav2020.orderforme.Database.Database.addToCart(Database.java:50)
        at com.dav2020.orderforme.FoodDetail$1.onClick(FoodDetail.java:57)
        at android.view.View.performClick(View.java:5610)
        at android.view.View$PerformClick.run(View.java:22265)
        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:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

This is the code where I use DB

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


public class FoodDetail extends AppCompatActivity {

    TextView food_name, food_price, food_description;
    ImageView food_image;
    CollapsingToolbarLayout collapsingToolbarLayout;
    FloatingActionButton btnCart;
    ElegantNumberButton numberButton;

    String foodId="";

    FirebaseDatabase database;
    DatabaseReference food;
    Food currentFood;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        //Init Firebase
        database = FirebaseDatabase.getInstance();
        food = database.getReference("food");

        //init view
        numberButton = (ElegantNumberButton)findViewById(R.id.number_button);
        btnCart = (FloatingActionButton)findViewById(R.id.btnCart);

        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Database(getBaseContext()).addToCart(new Order(
                        foodId,
                        currentFood.getName(),
                        numberButton.getNumber(),
                        currentFood.getPrice(),
                        currentFood.getDiscount()
                ));
                Toast.makeText(FoodDetail.this,"Added to Cart", Toast.LENGTH_SHORT).show();
            }
        });

        food_description = (TextView)findViewById(R.id.food_description);
        food_name = (TextView)findViewById(R.id.food_name);
        food_price = (TextView)findViewById(R.id.food_price);
        food_image = (ImageView)findViewById(R.id.img_food);

        collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing);
        collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
        collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);

        //get food ID from foodlist
        if(getIntent() != null)
            foodId = getIntent().getStringExtra("FoodId");
        if(!foodId.isEmpty()){
            getDetailedFood(foodId);
        }
    }



       private void getDetailedFood(String foodId) {
            food.child(foodId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    currentFood = dataSnapshot.getValue(Food.class);
                    Picasso.with(getBaseContext()).load(currentFood.getImage())
                            .into(food_image);
                    collapsingToolbarLayout.setTitle(currentFood.getName());
                    food_price.setText(currentFood.getPrice());
                    food_name.setText(currentFood.getName());
                    food_description.setText(currentFood.getDescription());



                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
    }
1
  • Tried to move DB file under my Java folder but app still crashes with the same error.
    – David
    Commented Apr 4, 2020 at 1:03

2 Answers 2

0

Actually you put DB file in wrong place Put your DB file and place in Java folder enter image description here

1
  • I have tried to move the assets folder under Java and under my project folder but both are not working..
    – David
    Commented Apr 4, 2020 at 1:55
0

Found the answer... the location was correct. assets folder must be located under main. The issue was my DB name. I set as below and realized that I forgot to add extension .db private static final String DB_NAME="OrderForMeDB";

after I change to below it works. private static final String DB_NAME="OrderForMeDB.db";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.