0

I am new to SQLite and when I run the application there is no .db file in

data/data/your.application.package/databases/

package com.navigationsystem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.SensorManager;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME="navigationsystem.db";
    public static final String NODE_ID="nodeID";

    public static final String CHILD_ID="childID";
    public static final String PARENT_ID="parentID";

    private SQLiteDatabase db;  

    public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    db.execSQL("CREATE TABLE nodes( nodeID CHAR(1) PRIMARY KEY );");
    db.execSQL("CREATE TABLE edges( childID CHAR(1) NOT NULL, parentID CHAR(1) NOT NULL,PRIMARY KEY(childID,parentID) );");

    this.db = this.getWritableDatabase(); 
    ContentValues cv=new ContentValues();

    cv.put(NODE_ID, "A");
    db.insert("nodes", NODE_ID, cv);
    cv.put(NODE_ID, "B");
    db.insert("nodes", NODE_ID, cv);
    cv.put(NODE_ID, "C");
    db.insert("nodes", NODE_ID, cv);
    cv.put(NODE_ID, "D");
    db.insert("nodes", NODE_ID, cv);
    cv.put(NODE_ID, "E");
    db.insert("nodes", NODE_ID, cv);
    cv.put(NODE_ID, "E");
    db.insert("nodes", NODE_ID, cv);


    cv.put(CHILD_ID, "A");
    cv.put(PARENT_ID, "C");
    db.insert("edges", CHILD_ID, cv);
    db.insert("edges", PARENT_ID,cv);

    cv.put(CHILD_ID, "B");
    cv.put(PARENT_ID, "E");
    db.insert("edges", CHILD_ID, cv);
    db.insert("edges", PARENT_ID,cv);

    cv.put(CHILD_ID, "C");
    cv.put(PARENT_ID, "D");
    db.insert("edges", CHILD_ID, cv);
    db.insert("edges", PARENT_ID,cv);

    cv.put(CHILD_ID, "C");
    cv.put(PARENT_ID, "F");
    db.insert("edges", CHILD_ID, cv);
    db.insert("edges", PARENT_ID,cv);
    db.close();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    android.util.Log.w("Constants", "Upgrading database, which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS constants");
    onCreate(db);
    }
}

What is wrong with my code? Is there any setting I have to do for this, or do I have to create another class for creation of a database file?

thanks.

1

2 Answers 2

1

Do you have any code that uses your DatabaseHelper class? If the DatabaseHelper is never used then its onCreate method will never be invoked, and hence no database will be created.

For example, if you're trying to create a content provider then the content provider will be started by Android when requests are sent to it (e.g., activity inserts data into the content provider). At startup the content provider's onCreate method is called and that is a good place to create the database helper instance, like

@Override
public boolean onCreate() {
    dbHelper = new DatabaseHelper(getContext());
    ...
}

Android won't magically create the database helper instance for us.

Now, I realize you may not be creating a content provider but need a database helper for other purposes. But the problem is probably the same - no code instantiates an instance of your DatabaseHelper class.

A more detailed description of your application is necessary to be more specific in what you need to do.

Sign up to request clarification or add additional context in comments.

Comments

0

You have provided the nice code. But you need to mention the code that you used in your activity.
Are you using the DatabaseHelper in your activity(Launcher) or not?
If you are not using it, then put this below statement inside activity oncreate() and run.

DatabaseHelper databaseHelper = new DatabaseHelper();

Now you can find your database file.

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.