【问题标题】:Air Sqlite Date in YYYY/MM/DD HH:MM FormatYYYY/MM/DD HH:MM 格式的 Air Sqlite 日期
【发布时间】:2011-08-24 02:08:24
【问题描述】:

我在玩 Air Sqlite 时,在数据库中保存日期时遇到了一些麻烦

INSERT INTO tblUserComments (comment_text, comment_cat,comment_date,parent_id) VALUES('"+bubbleText.text+"','"+chosenCat+"',DATETIME('now', 'localtime'),'"+_parentId+"')

以下列格式存储comment_date

2455783.2596296296 而不是 2011/08/09 18:13。

如何将日期数据保存为 YYYY/MM/DD HH:MM?

非常感谢。

【问题讨论】:

  • 你遇到了什么麻烦?保存日期是否有问题;或显示格式?难道你不只是想将数据库值用作新日期对象的值,然后使用 DateFormatter 来显示它吗?
  • 表格的结构如何? comment_date 列是否设置为 DATE?
  • @J_A_X,是的。 comment_date 列设置为 DATE。
  • @Flextras.com,例如,当我尝试执行“SELECT SELECT * FROM tblUserComments WHERE comment_date

标签: apache-flex sqlite air


【解决方案1】:

阅读后,您的列无法设置为 DATE,因为 SQLite 中没有这样的类型。引用开发手册:

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

我建议您将日期保存为 epoc 时间的整数,然后您可以在选择行时或在代码中进行转换。我个人更喜欢使用 Date 类根据时区正确转换它。 IE。 var date:Date = new Date(epocTime);

【讨论】:

  • 但是,当我使用以下命令时,AIR 编译器没有抛出任何错误:CREATE TABLE IF NOT EXISTS tblUserComments (comment_id INTEGER PRIMARY KEY AUTOINCREMENT,comment_text TEXT,comment_cat TEXT,comment_date DATETIME,parent_id INTEGER)跨度>
  • 编译器不会为此抛出错误,但可能在执行期间。或者也许 SQLite 只是将其默认为字符串。无论哪种方式,它都应该是一个 INTEGER。
【解决方案2】:

改为使用参数,因为它会自动调节许多问题,并将 sqlite 中的列类型设置为 DATE 并直接为其分配一个新的 Date() 对象或按以下格式为其分配一个字符串值:

private function dateToMySqlFormat(date:Date = null):String
{
    var formattedDate:DateFormatter = new DateFormatter();
    formattedDate.formatString = "YYYY-MM-DD JJ:NN:SS";

    if(date == null) date = new Date;

    return formattedDate.format(date);
}

应该可以的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多