我正在开发一个将名称、密码等存储到数据库的应用程序。而且我还想将当前日期和时间存储到数据库中并稍后检索。我无法破解如何在 SQLite 数据库中存储日期和时间。我应该使用日期时间功能吗?如果是,如何在我的后续示例中添加它。请帮忙。
提前致谢。
public class DBAdapter{public static final String ROWID = "_id";
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String PASSWORD = "password";
public static final String TYPE = "type";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "primus_db";
private static final String DATABASE_TABLE = "user_db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table user_db (_id integer primary key autoincrement, "
+ "name text not null, "
+ "email text not null, "
+ "password text not null," + "type text not null );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
/**
*
*/
public DBAdapter(Context ctx) {
this.context = ctx;
this.DBHelper = new DBAdapter.DatabaseHelper(this.context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
// ---insert a contact into the database---
public long insertContact(String name, String email, String password,
String type) {
ContentValues initialValues = new ContentValues();
initialValues.put(NAME, name);
initialValues.put(EMAIL, email);
initialValues.put(PASSWORD, password);
initialValues.put(TYPE, type);
return db.insert(DATABASE_TABLE, null, initialValues);
}
// ---retrieves all the contacts---
public Cursor getAllContacts() {
return db.query(DATABASE_TABLE, new String[] { ROWID, NAME, EMAIL,
PASSWORD, TYPE }, null, null, null, null, null);
}
}