1

The SQLite database file (.db) is not being created in Android Device Monitor >DBMS >File Explore >data. No .db file shown in this directory. I found myself unable to figure out why the database file is not created. here are my files.

MainActivity.java

package com.example.zohaib.database;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    SQLiteOpenHelper sqLiteOpenHelper;
    SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        sqLiteOpenHelper = new DBConnection(this);
        sqLiteDatabase = sqLiteOpenHelper.getWritableDatabase();

    }
}

DBConnection.java

package com.example.zohaib.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class DBConnection extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "products.db";
    private static final String TABLE_PRODUCTS = "products";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_PRODUCTNAME = "productName";



    public DBConnection(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            String query ="CREATE TABLE " + TABLE_PRODUCTS + " (" +
                        COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        COLUMN_PRODUCTNAME + " TEXT " +
                        ")";
            db.execSQL(query);
            Log.d("Create Database:", "Successful");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }
}
9
  • But the database still works as you expect? Can you still read and write it? Commented Feb 13, 2016 at 21:16
  • Also please post the commands you are using in your adb shell to check the location of the file. Commented Feb 13, 2016 at 21:17
  • @DougStevenson i use my mobile device as emulator. I open Android Device Monitor from toolbar of android studio. from this i click on my running device then DBMS > File explore > data . On clicking on data folder no any files shown Commented Feb 14, 2016 at 6:03
  • Do you get the "Create Database" log message? Commented Feb 14, 2016 at 7:32
  • if you are using real device as emulator you can't get your .db file in data -> data directory . Try inbuilt emulator to get .db file. Commented Feb 14, 2016 at 16:43

2 Answers 2

2

You are using two table names in one create table script.

Change

String query ="CREATE TABLE test_1 " + TABLE_PRODUCTS + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_PRODUCTNAME + " TEXT " +
                    ")";

to

String query ="CREATE TABLE " + TABLE_PRODUCTS + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_PRODUCTNAME + " TEXT " +
                    ");";
Sign up to request clarification or add additional context in comments.

Comments

1

Update: Just remove/uninstall the app from the device you are testing on and fix the query i.e. add the semicolon ";" at the end. Re-built the app.

or

you can use onUpgrade() to recreate the databse table by incrementing version. Also its not good to call lifecycle method method directly by you.

Working sample code (not perfect):

package com.example.zohaib.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBConnection extends SQLiteOpenHelper {
    // change version with change in schema or db changes
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "products.db";
    private static final String TABLE_PRODUCTS = "products";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_PRODUCTNAME = "productName";



    public DBConnection(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTable(db);
        Log.d("From onCreate : ", " successfully created.");
    }

    private void createTable(SQLiteDatabase db) {
        String query ="CREATE TABLE " + TABLE_PRODUCTS + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);
        Log.d("Create Database:", "Successful");

        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_PRODUCTNAME, "Sample Data");
        db.insert(TABLE_PRODUCTS, null, contentValues);
        Log.d("Insert Row:", "Insert Row Successful");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        createTable(db);
        Log.d("From onUpgrade : ", " successfully upgraded.");
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        createTable(db);
        Log.d("From onDowngrade : ", " successfully Downgraded.");
    }
}

Here Goes the Database files.

Here Goes the Database files.

5 Comments

MainActivity.this is only necessary when writing code inside an inner class. In sqLiteOpenHelper = new DBConnection(MainActivity.this);, this is just fine.
Why are you suggesting to insert data in onCreate()? This doesn't seem appropriate.
Activity contains context, so there is no need to use new DBConnection(MainActivity.this), new DBConnection(this) is OK.
@abcdef12 IMO, MainActivity.this causes confusion. This is not a standard practice. As for adding sample data, this certainly should be tested, but I don't think onCreate() is the appropriate place to do this. It also doesn't address the actual problem: the database doesn't seem to be created in the first place. This means that most likely there are already errors in the logcat if the OP will look.
Suggesting "_id" for the key is definitely good. This is only required by certain parts of the Android API but is a good practice in general even if you don't use those classes and/or methods.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.