我创建了一个DatabaseInterface
用于 Sqlite 操作的类。
DatabaseInterface.onCreate()
(我创建了一个表)当我创建这个类的一个对象时,方法没有被执行。
我DatabaseInterface
在另一个类中创建了对象FirstTimeActivity
public class FirstTimeActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseInterface db = new DatabaseInterface(this);
}
}
下面是类的定义DatabaseInteface
。该OnCreate()
方法没有被执行,所以我的表没有被创建。数据库已经创建。
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
}
};