我有两个字符串要插入到表“类别”中。字符串是“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");