Android无法将日期时间写入sqllite数据库

时间:2015-06-14 20:34:31

标签: java android sqlite datetime

我在我的应用中使用了Google Tasks API。我的应用程序中的一个字段"截止日期" requires a DateTime object。我使用Android datepicker dailog和EditText视图捕获截止日期,然后将用户输入转换为日期时间格式。当我尝试写入我的SQLite数据库时,我得到以下异常。

Error inserting due=2015-06-14T15:58:38.572-04:00 title=Test app _id=TaskId0.07429873487580996 status=needsAction notes=Test write
    android.database.sqlite.SQLiteException: table tasks has no column named due (code 1): , while compiling: INSERT INTO tasks(due,title,_id,status,notes) VALUES (?,?,?,?,?)

这令人困惑,因为我确定了一个名为" due"的列。在日志中还有另外一个例外。

06-14 15:58:38.607  15395-15395/com.github.idclark.forgetmenot E/EDITFRAGMENT﹕ null
    java.text.ParseException: Unparseable date: "06/14/15" (at offset 8)
            at java.text.DateFormat.parse(DateFormat.java:579)
            at com.github.idclark.forgetmenot.EditFragment.getTaskDueDate(EditFragment.java:64)

架构定义为

public static final class TaskEntry implements BaseColumns {

        public static final String TABLE_NAME = "tasks";
        public static final String COLUMN_TASK_ID = "_id";
        public static final String COLUMN_TASK_TITLE = "title";
        public static final String COLUMN_TASK_UPDATED = "updated";
        public static final String COLUMN_TASK_SELFLINK = "selfLink";
        public static final String COLUMN_TASK_PARENT = "parent";
        public static final String COLUMN_TASK_POSITION = "position";
        public static final String COLUMN_TASK_NOTES = "notes";
        public static final String COLUMN_TASK_STATUS = "status";
        public static final String COLUMN_TASK_DUE = "due";
        public static final String COLUMN_TASK_COMPLETED = "completed";
        public static final String COLUMN_TASK_DELETED = "deleted";
        public static final String COLUMN_TASK_HIDDEN = "hidden";

所以"到期"列确实存在。该表由

创建
@Override
    public void onCreate(SQLiteDatabase db) {

        //status must be either needsAction or completed

        final String CREATE_TASK_TABLE =
                "CREATE TABLE " + TaskEntry.TABLE_NAME + " (" +
                        TaskEntry._ID + " INTEGER PRIMARY KEY," +
                        TaskEntry.COLUMN_TASK_ID + "TEXT NOT NULL, " +
                        TaskEntry.COLUMN_TASK_TITLE + "TEXT NOT NULL, " +
                        TaskEntry.COLUMN_TASK_COMPLETED + "TEXT, " +
                        TaskEntry.COLUMN_TASK_NOTES + "TEXT, " +
                        TaskEntry.COLUMN_TASK_STATUS + "TEXT NOT NULL, " +
                        TaskEntry.COLUMN_TASK_DUE + "DATETIME, " +
                        TaskEntry.COLUMN_TASK_UPDATED + "DATETIME, " +
                        TaskEntry.COLUMN_TASK_PARENT + "TEXT, " +
                        TaskEntry.COLUMN_TASK_DELETED + "BOOLEAN, " +
                        TaskEntry.COLUMN_TASK_SELFLINK + "TEXT, " +
                        TaskEntry.COLUMN_TASK_POSITION + "TEXT, " +
                        TaskEntry.COLUMN_TASK_HIDDEN + "TEXT" + ")";

        db.execSQL(CREATE_TASK_TABLE);
    }

截止日期转换发生在此方法中。

public DateTime getTaskDueDate() {
    mDueDate = (EditText) getActivity().findViewById(R.id.task_due_date);
    return new DateTime(mDueDate.getText().toString());
}

然后使用

将数据最终写入数据库
public boolean insertRow(Task task) {
        ContentValues values = new ContentValues();
        values.put(TaskContract.TaskEntry.COLUMN_TASK_STATUS, task.getStatus());
        values.put(TaskContract.TaskEntry.COLUMN_TASK_ID,task.getId());
        values.put(TaskContract.TaskEntry.COLUMN_TASK_TITLE,task.getTitle());
        values.put(TaskContract.TaskEntry.COLUMN_TASK_DUE, task.getDue().toString());
        values.put(TaskContract.TaskEntry.COLUMN_TASK_NOTES, task.getNotes());

        SQLiteDatabase db = this.getWritableDatabase();
        boolean createSuccessful = db.insert(TaskContract.TaskEntry.TABLE_NAME, null, values) > 0;
        db.close();
        return createSuccessful;
    }

我很困惑为什么这个写失败,并且sql声称没有" due"柱。即使存在解析异常,我也会在日志中看到时间戳。在将数据写入数据库之前,我在日期时间对象上调用.toString()是问题的一部分吗?我没有很多sql的经验,我真的很困惑这些异常是什么。

1 个答案:

答案 0 :(得分:3)

对于sqlite问题,您需要在列名和类型之间使用空格。例如,更改

TaskEntry.COLUMN_TASK_DUE + "DATETIME, " +

TaskEntry.COLUMN_TASK_DUE + " DATETIME, " +

几乎所有的列都存在类似的问题。

修复CREATE TABLE SQL后,卸载并重新安装您的应用以重新创建数据库。

对于日期解析问题,请使用与您的数据匹配的日期格式。如果您需要详细的帮助,请发布一个新问题。