在EditText(MM-dd-yyyy)中格式化日期(yyyy-MM-dd)并将其写入sqlite

时间:2011-09-07 14:37:13

标签: android sqlite datetime date simpledateformat

在EditText中格式化日期,以西班牙语格式(MM-dd-yyyy)编写,并在sqlite数据库中以英文格式(yyyy-MM-dd)重写。

这是我的适配器代码:

public class AuxGlucDbAdapter {

public static final String KEY_IDPACIENTE = "IdPaciente";
public static final String KEY_FECHA = "Fecha";
public static final String KEY_EVE1 = "Eve1";
public static final String KEY_EVE2 = "Eve2";
public static final String KEY_EVE3 = "Eve3";
public static final String KEY_EVE4 = "Eve4";  
public static final String KEY_EVE5 = "Eve5";
public static final String KEY_EVE6 = "Eve6";
public static final String KEY_EVE7 = "Eve7";
public static final String KEY_ID = "_id";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(KEY_FECHA);
private static final String TAG = "AuxGlucDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;


private static final String DATABASE_CREATE =
    "create table AuxGluc (_id integer primary key autoincrement, "
    + "IdPaciente numeric not null, Fecha datetime not null, Eve1 numeric not null, Eve2 numeric not null, Eve3 numeric not null, Eve4 numeric not null, Eve5 numeric not null, Eve6 numeric not null, Eve7 numeric not null);";

private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "AuxGluc";
private static final int DATABASE_VERSION = 2;

private final Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE);
    }

    @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 AuxGluc");
        onCreate(db);
    }
}

public AuxGlucDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

public AuxGlucDbAdapter open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    mDbHelper.close();
}

public long createNote(String IdPaciente, String Fecha, String Eve1, String Eve2, String Eve3, String Eve4, String Eve5, String Eve6, String Eve7) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_IDPACIENTE, IdPaciente);
    initialValues.put(KEY_FECHA, Fecha);
    initialValues.put(KEY_EVE1, Eve1);
    initialValues.put(KEY_EVE2, Eve2);
    initialValues.put(KEY_EVE3, Eve3);
    initialValues.put(KEY_EVE4, Eve4);
    initialValues.put(KEY_EVE5, Eve5);
    initialValues.put(KEY_EVE6, Eve6);
    initialValues.put(KEY_EVE7, Eve7);

    return mDb.insert(DATABASE_TABLE, null, initialValues);
}

public boolean deleteNote(long Id) {

    return mDb.delete(DATABASE_TABLE, KEY_ID + "=" + Id, null) > 0;
}

public Cursor fetchAllNotes() {

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_IDPACIENTE, KEY_FECHA,
            KEY_EVE1, KEY_EVE2, KEY_EVE3, KEY_EVE4, KEY_EVE5, KEY_EVE6, KEY_EVE7}, null, null, null, null, null, null);
}

public Cursor fetchNote(long Id) throws SQLException {

    Cursor mCursor =

        mDb.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_IDPACIENTE,
                KEY_FECHA, KEY_EVE1, KEY_EVE2, KEY_EVE3, KEY_EVE4, KEY_EVE5, KEY_EVE6, KEY_EVE7}, KEY_ID + "=" + Id, null,
                null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

public boolean updateNote(long Id, String IdPaciente, String Fecha, String Eve1, String Eve2, String Eve3, String Eve4, String Eve5, String Eve6, String Eve7) {
    ContentValues args = new ContentValues();
    args.put(KEY_IDPACIENTE, IdPaciente);
    args.put(KEY_FECHA, Fecha);
    args.put(KEY_EVE1, Eve1);
    args.put(KEY_EVE2, Eve2);
    args.put(KEY_EVE3, Eve3);     
    args.put(KEY_EVE4, Eve4);   
    args.put(KEY_EVE5, Eve5); 
    args.put(KEY_EVE6, Eve6); 
    args.put(KEY_EVE7, Eve7); 

    return mDb.update(DATABASE_TABLE, args, KEY_ID + "=" + Id, null) > 0;
}
}

还有其他代码:

public class NoteEdit extends Activity {

private EditText mIdPacienteText;
private EditText mFechaText;
private EditText mEve1Text;
private EditText mEve2Text;
private EditText mEve3Text;
private EditText mEve4Text;
private EditText mEve5Text;
private EditText mEve6Text;
private EditText mEve7Text;
private Long mId;
private AuxGlucDbAdapter mDbHelper;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new AuxGlucDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.note_edit);
    setTitle(R.string.edit_note);

    mIdPacienteText = (EditText) findViewById(R.id.IdPaciente);
    mFechaText = (EditText) findViewById(R.id.Fecha);
    mEve1Text = (EditText) findViewById(R.id.Eve1);
    mEve2Text = (EditText) findViewById(R.id.Eve2);
    mEve3Text = (EditText) findViewById(R.id.Eve3);
    mEve4Text = (EditText) findViewById(R.id.Eve4);
    mEve5Text = (EditText) findViewById(R.id.Eve5);
    mEve6Text = (EditText) findViewById(R.id.Eve6);
    mEve7Text = (EditText) findViewById(R.id.Eve7);

    Button confirmButton = (Button) findViewById(R.id.confirm);

    mId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(AuxGlucDbAdapter.KEY_ID);
    if (mId == null) {
        Bundle extras = getIntent().getExtras();
        mId = extras != null ? extras.getLong(AuxGlucDbAdapter.KEY_ID)
                                : null;
    }

    populateFields();

    confirmButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
        }

    });
}

private void populateFields() {
    if (mId != null) {
        Cursor note = mDbHelper.fetchNote(mId);
        startManagingCursor(note);
        mIdPacienteText.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_IDPACIENTE)));
        mFechaText.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_FECHA)));
        mEve1Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE1)));
        mEve2Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE2)));
        mEve3Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE3)));
        mEve4Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE4)));
        mEve5Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE5)));
        mEve6Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE6)));
        mEve7Text.setText(note.getString(
                note.getColumnIndexOrThrow(AuxGlucDbAdapter.KEY_EVE7)));

    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    saveState();
    outState.putSerializable(AuxGlucDbAdapter.KEY_ID, mId);
}

@Override
protected void onPause() {
    super.onPause();
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    populateFields();
}

private void saveState() {
    String IdPaciente = mIdPacienteText.getText().toString();
    String Fecha = mFechaText.getText().toString();
    String Eve1 = mEve1Text.getText().toString();
    String Eve2 = mEve2Text.getText().toString();
    String Eve3 = mEve3Text.getText().toString();
    String Eve4 = mEve4Text.getText().toString();
    String Eve5 = mEve5Text.getText().toString();
    String Eve6 = mEve6Text.getText().toString();
    String Eve7 = mEve7Text.getText().toString();

    if (mId == null) {
        long Id = mDbHelper.createNote(IdPaciente, Fecha, Eve1, Eve2, Eve3, Eve4, Eve5, Eve6, Eve7);
        if (Id > 0) {
            mId = Id;
        }
    } else {
        mDbHelper.updateNote(mId, IdPaciente, Fecha, Eve1, Eve2, Eve3, Eve4, Eve5, Eve6, Eve7);
    }
}

}

1 个答案:

答案 0 :(得分:1)

Sqlite没有“datetime”类型。您最好存储Unix时间戳(我认为是date.getTime()) - 以毫秒为单位,并使用sqlite中的date(),time()和strftime()函数将其转换为sqlite中的任何内容(见下面的链接)。

请参阅this链接以了解sqlite数据类型。

请参阅this链接,了解如何在sqlite中设置日期格式。

另请参阅thisthis问题以获取更多帮助。

编辑:回答您的问题(来自上面的链接):

  

SQLite没有为存储日期留出的存储类   和/或时间。相反,SQLite的内置日期和时间函数   能够将日期和时间存储为TEXT,REAL或INTEGER   值:

     

TEXT as ISO8601 strings(“YYYY-MM-DD HH:MM:SS.SSS”)。真如朱利安   日数,11月以来格林威治中午以来的天数   公元前24,4714根据公历格里高利历。整数   as Unix Time,自1970-01-01 00:00:00 UTC以来的秒数。   应用程序可以选择在任何这些中存储日期和时间   使用内置日期格式和格式之间自由转换   时间函数。

因此,当您存储yyyy-MM-dd时,它实际上将其存储为TEXT字段(或字符串)。当你把它拉出来时它就像你存储它一样出现,但它是一个字符串,不能在没有Java解析的情况下作为日期进行操作。即使使用我的方法,您仍然需要解析日期,但您可以根据自己的喜好格式化查询中的日期(yyyy-MM-dd或MM-dd-yyyy)。

将日期(以毫秒为单位)存储为sqlite中的INTEGER的优点是,您现在可以在查询中对日期进行排序。

编辑:

示例:

您的数据库创建字符串如下所示:

private static final String DATABASE_CREATE =
    "create table AuxGluc (_id integer primary key autoincrement, "
    + "IdPaciente numeric not null, Fecha integer not null, Eve1 numeric not null, Eve2 numeric not null, Eve3 numeric not null, Eve4 numeric not null, Eve5 numeric not null, Eve6 numeric not null, Eve7 numeric not null);";

现在,当您保存日期时,可以使用Androids Calendar类,并调用myCalendar.getTimeInMillis()来获取整数值。所以你的createNode签名看起来像这样:

public long createNote(String IdPaciente, int Fecha, String Eve1, String Eve2, String Eve3, String Eve4, String Eve5, String Eve6, String Eve7) {...}