将时间插入sqlite时出错

时间:2013-04-20 19:40:09

标签: android sqlite simpledateformat

我有两个字符串,我试图插入表“类别”。字符串是“updatedAt”和“createdAt”。我把它们作为简单日期格式的字符串:

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

我收到错误,因为时间包含冒号。至少我相信这就是正在发生的事情。这是我的插入代码:

public void insertCategories(SQLiteDatabase db, int id, String group, String updatedAt, String createdAt){

        String CATEGORY_INSERTION = 
            "insert into " + DATABASE_USERTAG + "(" + KEY_TAGID + ", " + KEY_TAGNAME + ", " + KEY_UPDATED_AT_TAG + ", " + KEY_CREATED_AT_TAG +") " +
            "values(" + id + ", " + group + ", " + updatedAt + ", " + createdAt +");";
        db.execSQL(CATEGORY_INSERTION);
    }

这是logcat输出:

    04-20 12:10:38.489: E/Database(629): Failure 1 (near "12": syntax error) on 0x1b8268 when preparing 'insert into UserTag(_id, tagName, udpatedAtTag, createdAtTag) values(1, 'Favorites', 2013-04-20 12:10:38, 2013-04-20 12:10:38);'.
    04-20 12:10:38.519: W/dalvikvm(629): threadid=9: thread exiting with uncaught exception (group=0x40015560)
    04-20 12:10:38.529: E/AndroidRuntime(629): FATAL EXCEPTION: ModernAsyncTask #1
    04-20 12:10:38.529: E/AndroidRuntime(629): java.lang.RuntimeException: An error occured while executing doInBackground()
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.lang.Thread.run(Thread.java:1019)
    04-20 12:10:38.529: E/AndroidRuntime(629): Caused by: android.database.sqlite.SQLiteException: near "12": syntax error: insert into UserTag(_id, tagName, udpatedAtTag, createdAtTag) values(1, 'Favorites', 2013-04-20 12:10:38, 2013-04-20 12:10:38);
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at mcbee.recipeBoXx.honeyComb.RecipeDbAdapterMod$DatabaseHelper.insertCategories(RecipeDbAdapterMod.java:180)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at mcbee.recipeBoXx.honeyComb.RecipeDbAdapterMod$DatabaseHelper.onCreate(RecipeDbAdapterMod.java:160)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at mcbee.recipeBoXx.honeyComb.RecipeDbAdapterMod$DatabaseHelper.onUpgrade(RecipeDbAdapterMod.java:173)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at mcbee.recipeBoXx.honeyComb.CategoryContentProvider.query(CategoryContentProvider.java:88)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.content.ContentResolver.query(ContentResolver.java:262)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
    04-20 12:10:38.529: E/AndroidRuntime(629):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    04-20 12:10:38.529: E/AndroidRuntime(629):  ... 4 more

此外,如果我将简单的日期格式更改为:

SimpleDateFormat mSdf = new SimpleDateFormat("yyyy-MM-dd");

没有错误。如何正确格式化此字符串以便包含时间?

编辑:想出来,应该想到这个......将简单的日期格式改为:

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

2 个答案:

答案 0 :(得分:0)

以毫秒为单位存储时间。然后检索数据只需将毫秒时间作为长值,然后进行一些计算以将其转换为格式化时间。

答案 1 :(得分:0)

你忘记了字符串周围的'引号'。

您可以使用以下内容:

... ", '" + createdAt + "', '" + ...

但是使用insert函数要容易得多:

ContentValues cv = new ContentValues();
cv.put(KEY_TAGID, id);
cv.put(KEY_TAGNAME, group);
cv.put(KEY_UPDATED_AT_TAG, updatedAt);
cv.put(KEY_CREATED_AT_TAG, createdAt);
db.insert(DATABASE_USERTAG, null, cv);