Question

I tried to use SQLite on Android. I want to see my database file. So, I use Android File Explorer and browse this link: Data\data\App_name But in Data\data. I cannot see my app name. (in below code will be: com/app/TimeTracker)

Here my code to test:

first is my main program:

package com.app;
    public class TimeTrackerActivity extends Activity {    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TimeTrackerDatabase database = new TimeTrackerDatabase(this);
        }
    }

and second is my database helper:

package com.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TimeTrackerDatabase extends SQLiteOpenHelper {

    public TimeTrackerDatabase(Context context){
        super(context, "timetracker.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(
                "CREATE TABLE timerecords"+"id INTEGER PRIMARY KEY, time TEXT, notes TEXT)"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    }

}

Is there any problem in above two file ? If has, please tell me what. And if not, please tell me how to fix this problem.

thanks :)

@Edit: I add my screenshot of my data folder. (No file in lib folder)

Screenshot

Was it helpful?

Solution

the database related issue will be solved by below updation in your code.

package com.app;
    public class TimeTrackerActivity extends Activity {    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TimeTrackerDatabase database = new TimeTrackerDatabase(this);
SQLiteDatabase db = database.getWritableDatabase();   //Add this line.
        }
    }

public class TimeTrackerDatabase extends SQLiteOpenHelper {

    public TimeTrackerDatabase(Context context){
        super(context, "timetracker.db", null, 2);

    }


@Override
    public void onCreate(SQLiteDatabase database) {

     database.execSQL(

                "CREATE TABLE timerecords" +
                "(id INTEGER PRIMARY KEY, time TEXT, notes TEXT)"

        );

    }

Regarding your app not visible in File explorer, you need to search by package name as below data/data/com.app and here you can see database folder where you can see all your databases created by applicatioenter image description heren

OTHER TIPS

actually your database is not being created due to faulty query.

write your query as:

"CREATE TABLE IF NOT EXISTS timerecords
    (id INTEGER PRIMARY KEY, time TEXT, notes TEXT);"

once, the db file is created, then you can browse till the desired location

It's not Data\data\App_name , it'll be Data\data\package_name. So in your case it'll be Data\data\com.app\ Try looking your db file there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top