插入的数据与发送的实际数据不同

时间:2018-06-28 17:14:05

标签: python sql postgresql

我正在尝试将数据插入到Postgres数据库中,我能够插入一些其他数据,但不是实际数据

这是我查询执行功能中的数据生成器和数据发送器( (Python-Falsk)

def runid():
    cmdStr = 'cd /root/Dart/log; ls -rt DartRunner*.log | tail -1 '
    ret = execCmdLocal(cmdStr)
    logName = ret[1].strip()
    runId = ""
    print('The DartRunner Log generated is: %s'%logName)
    with open('/root/Dart/log/' + logName, "r") as fd:
         for line in fd:
            if 'runId' in line:
               runId = line.split()[-1]
               print('Run Id: %s'%runId)
               break
    print (runId) # output : Run Id: 180628-22
    post_runid(runId) # output is given in below link
    return jsonify({"run_id": runId})

这是我的数据库(postgres)执行方法: (Python)

 def post_runid(run_id):
     query = "insert into runid(runid) values(%s)"
     cur.execute(query %run_id)
     conn.commit()

我的输出看起来像这样: The above two rows are manually inserted by me but the below two rows are executed from the code, the below two rows must be same as the above ones but for some reason they are not as the original data but being generated in series

1 个答案:

答案 0 :(得分:0)

在查询中将%s更改为'%s'可以解决问题。

def post_runid(run_id):
     query = "insert into runid(runid) values('%s')"
     cur.execute(query, (run_id,))
     conn.commit()