【问题标题】:Powershell writing to MySQL table column with datetime datatypePowershell 使用 datetime 数据类型写入 MySQL 表列
【发布时间】:2015-06-28 22:40:18
【问题描述】:

我有一个 MySQL 数据库,我正在使用 Powershell 更新其中的数据。我可以更新到任何文本列,但我遇到了数据类型为 datetime 的“时间”列的问题。根据 MySQL 文档,日期时间格式为 YYYY-MM-DD HH:MM:SS (https://dev.mysql.com/doc/refman/5.1/en/datetime.html)

以该格式获取日期/时间的等效 powershell 命令是: 获取日期格式 G (https://technet.microsoft.com/en-us/library/ee692801.aspx)

当我将该时间写入变量并尝试更新记录时,记录保持空白,并且我假设数据类型已关闭,因为我可以写入整数和文本列。

有人知道如何确保正确的数据类型以便我可以更新记录吗?

【问题讨论】:

    标签: mysql datetime powershell


    【解决方案1】:

    试试

    get-date -Format "yyyy-MM-dd hh:mm:ss"
    

    【讨论】:

      【解决方案2】:

      之前的答案很接近,但小时必须是“HH”而不是“hh”,否则您最终会以 AM 插入/查询所有 PM 时间。

      # Gets the current datetime in correct format for a MySQL query
      [string]$mySqlDateTime = get-date -Format "yyyy-MM-dd HH:mm:ss"
      
      # Assuming you have a [datetime] variable named $datetime,
      # use the following to get the correct format for a MySQL query
      [string]$mySqlDateTime = $datetime.ToString("yyyy-MM-dd HH:mm:ss")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        相关资源
        最近更新 更多