mysql - Powershell 使用日期时间数据类型写入 MySQL 表列

标签 mysql datetime powershell

我有一个 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/

相关文章:

c# - 使用PInvoke捕获远程计算机的CD Rom信息

导出后Mysql数据库表为空

类似于 Facebook 的 PHP/MySQL 过滤搜索

python - 将 24 小时日期/时间转换为 12 小时格式,反之亦然

sqlite - 使用 SQLite 进行透视查询

json - 在Powershell中从JSON转换为CSV

php - mysql 无法在 xampp 上运行

java - 具有相同列名的命名查询内连接表的重复数据/值

php - 类型错误:DateTime::__construct() expects parameter 1 to be string, object given in laravel

powershell - 将参数从 ps1 发送到其他 ps1