0

I have create a class DatabaseInterface which will be used for Sqlite operations. DatabaseInterface.onCreate() (where I have created a table) Method is not getting executed when i create an object of this class.

I have created object of DatabaseInterface in another class FirstTimeActivity

public class FirstTimeActivity extends Activity{

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DatabaseInterface db = new DatabaseInterface(this);
    }
}

Below is class Definition of DatabaseInteface. The OnCreate() method is not getting executed, so my table is not getting created. The database has already been created.

public class DatabaseInterface  extends SQLiteOpenHelper{

    private static String       DB_NAME = "SMS_DB";
    private static int          DB_VERSION = 1;

    private String TABLE_NAME       = "SENT_ITEMS";
    private String COLUMN_ID        = "S_NO";
    private String COLUMN_MESSAGE   = "MESSAGE";
    private String COLUMN_DATETIME  = "DATE_TIME";

    //creates the database
    public DatabaseInterface(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            Log.d("Creating Database: ", "Creating Database");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            Log.d("Creating Table: ", "Creating Table");
            String create_table = "CREATE TABLE "+ TABLE_NAME + " ("+ COLUMN_ID +" INTEGER PRIMARY KEY, " + COLUMN_MESSAGE + " TEXT, " + COLUMN_DATETIME + "TEXT NOT NULL); ";
            db.execSQL(create_table);
    }

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

    }

    public void insertIntoDB(String message)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    //get current date and time
    Calendar current_time = Calendar.getInstance();
    //current_time.getTime();
    String Cure = current_time.toString();
    Log.d("Current_Date_time: ", Cure);

    values.put(COLUMN_MESSAGE, message);
    values.put(COLUMN_DATETIME, "ad");

    db.insert("TEST", null, values);
    db.close(); // Closing database connection
}

};
0

2 Answers 2

2

The database isn't actually created until you call getWritableDatabase() on the DatabaseInterface object

** EDIT **

With the new info it seems that the problem is that your DB is already created, hence the OS doesn't need to execute the onCreate, as it's already created. Either rethink how you are creating your DB to include table creation at the same time, or just insert the table manually if it doesn't exist.

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

2 Comments

I thought it was only on openOrCreate and similar when the database is actually created. As opposed to referenced.
@Martyn i missed insertIntoDB() method earlier where i have called SQLiteDatabase db = this.getWritableDatabase();
0

I had the same problem. It was solved by deleting and re-installing the App on the phone emulator:

I had bugs in my code when I execute db.getWritableDatabase() for the first time, the database didn't get created successfully. There was no .db file under the App folder if I browse in the Android Device Monitor, so I thought the OnCreate would be called after I fix my bug. -- It didn't. Then I uninstalled the app on the emulator, and re-installed with the correct version of code, the Oncreate function was called and the .db file was created successfully.

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.