1

I currently working on my school assignment. I currently facing some issue on creating a SQLITE DB when debugging my app.

logcat:

E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/data/data/com.asus.microfilm/databases/Sugar.db) -
E/SQLiteDatabase﹕ Failed to open database '/data/data/com.asus.microfilm/databases/Sugar.db'.

I wondering why it open up the other package DB. I have search some example online and I couldn't find any solution that's why I decided to post it here.

Besides, I have already used adb-shell and I found nothing in my package.

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

tab1.java

DatabaseHelper myDb;
myDb = new DatabaseHelper(getActivity());

my tab1.java is a fragment.

10
  • updated on the context.getExternalFilesDir I want to save it to my SDcard but when i open up my package>files in DDMS still nothing Commented Nov 11, 2015 at 10:22
  • what you want to do ? Commented Nov 11, 2015 at 10:30
  • You want to save the db else where ? Commented Nov 11, 2015 at 10:30
  • I'm trying to save my *.db file into my SD card. Due to my phone is not rooted. I unable to access the data>data folder in my internal storage Commented Nov 11, 2015 at 10:33
  • who said you that you can not access the data> data folder ? Commented Nov 11, 2015 at 10:35

4 Answers 4

1

my solution for example

public class NotesDB extends SQLiteOpenHelper {


    private static final String DB_NAME = "NotesFile";
    private static final String LOG_TAG = "myLogs";
    private static final String TABLE_NAME = "Notes";
    private static final String COLUMN_KEY = "key";
    private static final String COLUMN_NOTE = "note";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_QUESTION = "question";
    private String table = "";

    public NotesDB(Context context) {
        super(context, DB_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(LOG_TAG, "--- onCreate database ---");
        db.execSQL("create table "+TABLE_NAME+" ("
                + "id integer primary key autoincrement,"
                + COLUMN_NAME+" text,"
                + COLUMN_KEY+" integer,"
                + COLUMN_QUESTION+" text,"
                + COLUMN_NOTE+" text" + ");");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

usages

SQLiteDatabase db = new NotesDB(getAppContext()).getWritableDatabase();
        c=db.query("Notes", null, selection, selectionArgs , null, null, null );................
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @latsenko Anton, I tried it. Can't works! When I launch the app, straight away show stopped. :(
1

try this:

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

1 Comment

Hi @walkmn, I had tried this. It does not work and I don't know why. When I check my logcat back it shows /data/data/com.asus.microfilm/databases/Sugar.db which is totally different from my package.
0

Try this and let me know if it creates the file there

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, context.getExternalFilesDir("MyDatabase") + "/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

or you can try this too if want to use internal memory , as its more secure

public class DatabaseHelper extends SQLiteOpenHelper 
{
    public DatabaseHelper(Context context) 
    {
        super(context, context.getFilesDir() + "/" + DATABASE_NAME, null, DATABASE_VERSION);
    }

Update 1: Try Using this

 public DatabaseHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + FILE_DIR
            + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}

16 Comments

Nope, checked by using DDMS. :(
in which directory you are looking for ?
storage>MicroSD>Android>com.user.myassignment2v04>Files, am i correct?
no You are searching in wrong directory , may be the databse is creating by the solution I suggested but as you are searching in wrong path You can not find it , ,
I have added the Log so that you can see what is the exact path , and also share me that path in the comment
|
0

SQLiteOpenHelper have two constructor:

SQLiteOpenHelper(Context context, String name,    SQLiteDatabase.CursorFactory factory, int version)


SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)

You have to implement one of them,and create tables in onCreate method.

Comments

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.