0

目前我的 SQLite 数据库运行良好,我已经阅读了各种方法,例如实现目的的方法,尽管目前我在设置 Date 构造函数以及如何定义数据以将其放入数据库时​​遇到问题(并取回它!)

到目前为止,我已经使用 static 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;
    }
}

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

4

1 回答 1

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());
于 2013-05-09T22:27:32.303 回答