English 中文(简体)
如何在Android下正确地在SQLite中插入日期时间值?
原标题:How to corectly insert a datetime value in SQLite under Android?

我正在做一个虚拟的安卓项目来掌握一些东西。以下代码行在没有任何错误的情况下破坏了我的android应用程序。我使用Idea作为IDE。

database.execSQL("Insert Into todos(uid, name, created_on, changed_on) values( 6a7047ed-c2f9-407f-a774-1c02b0fe9caf ,  todo , DATETIME( NOW ), DATETIME( NOW ))");

我确信数据库有给定的列,如果我只是使用:

database.execSQL("Insert Into todos(uid, name) values( 6a7047ed-c2f9-407f-a774-1c02b0fe9caf ,  todo )");

应用程序以应有的方式工作。

我试图插入这样的日期(基于与此事相关的已回答问题):

  2012-05-25 16:52:00 

 date( now )

I get the same crash with no err或 或 debug log.

我在这里错过了什么?

已更新

根据请求,用于创建数据库表的代码。

    public static final String TABLE_TODOS = "todos";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_UID = "uid";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_CHECKED = "checked";
    public static final String COLUMN_TO_DELETE = "must_delete";
    public static final String COLUMN_CHANGED_ON = "changed_on";

    public static final String COLUMN_CREATED = "created_on";

    private static final String DATABASE_CREATE = "create table "
            + TABLE_TODOS + "( "
            + COLUMN_ID + " integer primary key autoincrement, "
            + COLUMN_UID + " varchar(50), "
            + COLUMN_NAME + " text not null, "
            + COLUMN_CHECKED + " integer default 0, "
            + COLUMN_TO_DELETE + " integer default 0, "
            + COLUMN_CHANGED_ON + " datetime "
            + COLUMN_CREATED + " datetime "
            +");";
最佳回答

您的问题是,您声明的数据类型在SQlite中不存在。从文档中:

“1.2日期和时间数据类型

SQLite没有专门用于存储日期和/或时间的存储类。相反,SQLite的内置日期和时间函数能够将日期和时间存储为TEXT、REAL或INTEGER值:

文本为ISO8601字符串(“YYYY-MM-DD HH:MM:SS.SSS”)。真正的儒略历日数,根据保守的格里高利历,自公元前4714年11月24日格林尼治中午以来的天数。INTEGER表示Unix时间,自1970-01-01 00:00:00 UTC以来的秒数。

应用程序可以选择以这些格式中的任何一种存储日期和时间,并使用内置的日期和时间函数在格式之间自由转换。"

因此,在数据库中更改数据类型,重新创建它,并更改从这些列插入/更新/检索数据的方法,以匹配正确的数据类型,这样对你来说会更好。

问题回答

在表create语句中,created_on列前缺少一个逗号。

+ COLUMN_CHANGED_ON + " datetime "

应该是

+ COLUMN_CHANGED_ON + " datetime, "

根据他们自己的SQLite文档,将使用正确的数据类型NUMERIC来记录DATETIMEhttp://www.sqlite.org/datatype3.html遵循关联数据类型的规则。请参阅亲和力表。

COLUMN_CHANGED_ON + " datetime "

应该是

COLUMN_CHANGED_ON + " NUMERIC, "

使用正确的关联数据类型,您可以比较值,例如。

SELECT b <  40 , b <  60 , b <  600  FROM t1;




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签