J4X
J4X

Reputation: 119

creating sqlite database on device using AndroidStudio

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.

Upvotes: 1

Views: 441

Answers (4)

Coas Mckey
Coas Mckey

Reputation: 709

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);
}

Upvotes: 0

walkmn
walkmn

Reputation: 2391

try this:

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

Upvotes: 1

Foocoder
Foocoder

Reputation: 11

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.

Upvotes: 0

Iatsenko Anton
Iatsenko Anton

Reputation: 157

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 );................

Upvotes: 1

Related Questions