在Python中构建多行数据库查询

Ben*_*enH 5 mysql python-2.7

遇到了一个问题,而且一开始看起来很简单。

我想这样做(为了简洁起见,查询已被简化,它实际上更长且更复杂):

    query  = ("""INSERT INTO %s " % cfg['mysql.table']
              "('hostname', 'timestamp', 'metric', 'average', 'peakhigh', 'peaklow', 'gsamp', 'zsamp', 'nsamp')"""
              "VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s )"
             )   
Run Code Online (Sandbox Code Playgroud)

那么如何将 STR_TO_DATE 合并到时间戳字段的查询中呢?

谢谢你的帮助。

小智 7

将字符串放在三引号内:

query  = ("""INSERT INTO %s " % cfg['mysql.table']
              "('hostname', 'timestamp', 'metric', 'average', 'peakhigh', 'peaklow', 'gsamp', 'zsamp', 'nsamp')"
              "VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s )"""
             ) 
Run Code Online (Sandbox Code Playgroud)