Reputation: 255
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
}
};
Upvotes: 0
Views: 1334
Reputation: 1
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.
Upvotes: 0
Reputation: 16622
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.
Upvotes: 2