我有一个 MySQL 数据库,我正在使用 Powershell 更新数据。我可以更新到任何文本列,但我遇到了数据类型为日期时间的“时间”列的问题。根据 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 )
当我将该时间写入变量并尝试更新记录时,记录保持空白,并且我假设数据类型已关闭,因为我可以写入整数和文本列。
有人知道如何确保正确的数据类型以便我可以更新记录吗?
最佳答案
前面的答案很接近,但小时需要是“HH”而不是“hh”,否则您最终将所有 PM 时间都作为 AM 插入/查询。
# 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")
关于mysql - Powershell 使用日期时间数据类型写入 MySQL 表列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777806/