我编写了这个简单的脚本来更新 MySQL 中的表。为此,我创建了一个 for 循环并尝试了以下操作(键盘链接):
sUpdate = [[UPDATE `latest`
SET `date` = '%s'
WHERE `date` = '%s'
AND `fid` > 50000]]
for i = 1, 12 do
print( i )
sOldDate = "2009-"..tostring(i).."-10"
sNewDate = "2010-09-"..tostring(i)
sUpdate = string.format( sUpdate, sNewDate, sOldDate )
print( sUpdate )
end
输出如下:
1
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
2
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
3
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
4
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
5
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
6
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
7
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
8
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
9
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
10
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
11
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
12
UPDATE `latest`
SET `date` = '2010-09-1'
WHERE `date` = '2009-1-10'
AND `fid` > 50000
如您所见,print(i)
打印效果很好,但是 thesOldDate
和sNewDate
have i 都被视为1。然后我改变了sOldDate
和sNewDate
如下:
sOldDate = string.format("2009-%d-10", i)
sNewDate = string.format("2010-09-%d", i)
而且我仍然得到两个日期的输出:2009-1-10和2010-09-1如此处所示。
这个循环可能有什么问题。我在这样的循环上工作了很长时间,直到今天他们从未让我失望过。
我认为这只是我无法识别的一些愚蠢的错误。任何帮助表示赞赏。