Android将DATE(现在)添加到SQLite数据库

时间:2013-05-09 13:36:07

标签: java android sqlite datetime

目前我的SQLite数据库运行良好,我已经阅读了this one这样的各种方法来实现这一点,尽管目前我遇到了设置Date构造函数以及如何定义数据以供使用的问题将它放入数据库(并将其取回!)

到目前为止,我已经使用静态final String来定义我的数据变量,不确定哪种是使用Date的正确方法。

我不关心时间,只关注创建条目的日期。

下面是我的java类:

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Stats {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_WEIGHT = "weight";
    public static final String KEY_WAIST = "waist";
    public static final String KEY_CHEST = "chest";
    public static final String KEY_LEGS = "legs";
    public static final String KEY_ARMS = "arms";
    public static final String KEY_DATE = "date";


    private static final String DATABASE_NAME = "statsDB";
    private static final String DATABASE_TABLE = "personalStats";
    private static final int DATABASE_VERSION = 3;

    private DbHelper ffHelper;
    private final Context ffContext;
    private SQLiteDatabase ffDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_WEIGHT
                    + " TEXT NOT NULL, " + KEY_WAIST + " TEXT NOT NULL, "
                    + KEY_CHEST + " TEXT NOT NULL, " + KEY_LEGS
                    + " TEXT NOT NULL, " + KEY_ARMS + " TEXT NOT NULL, " 
                    + KEY_DATE + " TEXT NOT NULL);");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);

        }
    }

    public Stats(Context c) {
        ffContext = c;
    }

    public Stats open() throws SQLException {
        ffHelper = new DbHelper(ffContext);
        ffDatabase = ffHelper.getWritableDatabase();
        return this;

    }

    public void close() {
        ffHelper.close();

    }

    public long createEntry(String weight, String waist, String chest, String legs, String arms) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        Date date = new Date();
        ContentValues cv = new ContentValues();
        cv.put(KEY_WEIGHT, weight);
        cv.put(KEY_WAIST, waist);
        cv.put(KEY_CHEST, chest);
        cv.put(KEY_LEGS, legs);
        cv.put(KEY_ARMS, arms);
        cv.put("date_created", dateFormat.format(date));
        return ffDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
        String[] columns = new String[] { KEY_ROWID, KEY_WEIGHT, KEY_WAIST, KEY_CHEST, KEY_LEGS, KEY_ARMS, KEY_DATE };
        Cursor c = ffDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";
        int iRow = c.getColumnIndex(KEY_ROWID);
        int iWeight = c.getColumnIndex(KEY_WEIGHT);
        int iWaist = c.getColumnIndex(KEY_WAIST);
        int iChest = c.getColumnIndex(KEY_CHEST);
        int iLegs = c.getColumnIndex(KEY_LEGS);
        int iArms = c.getColumnIndex(KEY_ARMS);
        int iDate = c.getColumnIndex(KEY_DATE);
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(iRow) + " " + c.getString(iWeight)
                    + " " + c.getString(iWaist) 
                    + " " + c.getString(iChest) 
                    + " " + c.getString(iLegs) 
                    + " " + c.getString(iArms)
                    + " " + c.getString(iDate)+ "\n";

        }

        return result;
    }
}

显然,任何时候提到日期都是我试图添加这个额外功能的地方。

1 个答案:

答案 0 :(得分:0)

您可以在数据库中输入字符串之类的日期,但此字符串必须具有良好的格式。 对于SQLite,它是:

YYYY-MM-dd
YYYY-MM-dd hh:mm
YYYY-MM-dd hh:mm:ss
YYYY-MM-dd hh:mm:ss.sss
YYYY-MM-DDThh:mm
YYYY-MM-DDThh:mm:ss
YYYY-MM-DDThh:mm:ss.sss
hh:mm
hh:mm:ss
hh:mm:ss.sss

与Yoann一样,您可以将日期转换为字符串:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateForinsert = dateFormat(new GregorianCalendar(2013, GregorianCalendar.MAY, 5).getTime());

“现在”您可以设置:

String dateForinsert = dateFormat(new GregorianCalendar().getTime());