因此,这是其中包含 SQL 查询的行。如何在没有 python 尝试用字符串替换 %s 的情况下使用 strftime('%s', 'now') ......这非常烦人。
谢谢
DBQuery.query('INSERT INTO test (Date, Entry) VALUES (strftime(''%ss'', ''now''), "%s")' %('%', cherrypy.request.params['username']), 'INSERT')
因此,这是其中包含 SQL 查询的行。如何在没有 python 尝试用字符串替换 %s 的情况下使用 strftime('%s', 'now') ......这非常烦人。
谢谢
DBQuery.query('INSERT INTO test (Date, Entry) VALUES (strftime(''%ss'', ''now''), "%s")' %('%', cherrypy.request.params['username']), 'INSERT')
您可以%
通过使用两个背靠背来逃避 ,因此将%%s
字符串放入您希望它变为. 的位置%s
。例如:
>>> print '''strftime('%%s', 'now'), "%s"''' % "some value"
strftime('%s', 'now'), "some value"
另外,我认为您的背靠背单引号并没有像您认为的那样做。这不会在字符串中留下单引号,它只会将两个字符串连接在一起,例如:
>>> 'aaa''bbb'
'aaabbb'
相反,您应该使用三引号字符串或转义单引号。最终结果可能如下所示:
DBQuery.query('''INSERT INTO test (Date, Entry) VALUES (strftime('%%s', 'now'), "%s")''' % cherrypy.request.params['username'], 'INSERT')