【问题标题】:Inserted data is different from actual data being sent插入的数据与发送的实际数据不同
【发布时间】:2018-06-28 17:14:04
【问题描述】:

我正在尝试将数据插入到我的 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

【问题讨论】:

  • 试试cur.execute(query, run_id),这样可以避免sql注入。否则将您的查询更改为values('%s'),这样就不会出现格式问题!

标签: python sql postgresql


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 2019-11-07
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-18
    • 2014-11-13
    相关资源
    最近更新 更多