Python MySQL查询将创建的\u替换为更新的\u
我有以下Python MySQL查询:Python MySQL查询将创建的\u替换为更新的\u,python,mysql,Python,Mysql,我有以下Python MySQL查询: sql = "INSERT INTO Tweets(timestamp, message_id, body, username, symbol_id, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE timestamp=VALUES(timestamp), message_id=VALUES(message_i
sql = "INSERT INTO Tweets(timestamp, message_id, body, username, symbol_id, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s') ON DUPLICATE KEY UPDATE timestamp=VALUES(timestamp), message_id=VALUES(message_id), body=VALUES(body), username=VALUES(username), symbol_id=VALUES(symbol_id), created_at=NOW(), updated_at=NOW()"% \
(timestamp, messageId, body, username, dbID, currentTime, currentTime)
一切都很好,直到我点击DUPLOCATE KEY UPDATE上的,其中updated_at
被替换为NOW()
时间戳。我试图删除currentTime
和updated\u at=NOW()
,但会出现以下错误:
Traceback (most recent call last):
File "./parseTweets.py", line 50, in <module>
(timestamp, messageId, body, username, dbID, currentTime)
TypeError: not enough arguments for format string
回溯(最近一次呼叫最后一次):
文件“/parseTweets.py”,第50行,在
(时间戳、messageId、正文、用户名、dbID、currentTime)
TypeError:格式字符串的参数不足
如果只是更新行,我不希望将updated_at
替换为created_at
,因此从UPDATE
子句中删除updated_at=NOW()
?另外,这很容易受到攻击。谢谢@univerio-我该如何修复SQL注入漏洞?在DBAPI调用中使用参数。执行调用,而不是游标。执行(“插入tbl(foo)值('%s');%foo)
,执行游标。执行(“插入tbl(foo)值(%s);”,(foo,)
。