JSON和MySQL UPDATE查询

时间:2016-07-20 08:32:14

标签: python mysql json

我正试图通过Python从ZeroMQ的JSON消息中获取信息到MySQL。这是我试图运行的代码片段:

upstream app {
  server 127.0.0.1:9090;
  keepalive 64;
}

upstream api {
  server 127.0.0.1:8081;
  keepalive 64;
}

#
# The default server
#
server {
  listen   80;
  server_name  _;

  location /api {
    rewrite /api/(.*) /$1  break;
    proxy_pass http://api;
    proxy_redirect off;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host   $http_host;
    proxy_set_header   X-NginX-Proxy true;
    real_ip_header     X-Forwarded-For;
        real_ip_recursive  on;
    #proxy_set_header   Connection "";
    #proxy_http_version 1.1;
  }

  location /{
    rewrite /(.*) /$1  break;
    proxy_pass http://app;
    proxy_redirect off;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host   $http_host;
    proxy_set_header   X-NginX-Proxy true;
    real_ip_header     X-Forwarded-For;
        real_ip_recursive  on;
    #proxy_set_header   Connection "";
    #proxy_http_version 1.1;
  }

  # redirect not found pages to the static page /404.html
  error_page  404  /404.html;
  location = /404.html {
    root   /usr/share/nginx/html;
  }

  # redirect server error pages to the static page /50x.html
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

但是它又回来了:

  

1064,'你的SQL语法错误

此时我真的可以使用第二双眼睛,我觉得我完全错过了它。

抛出sql变量返回:

for i in json_msg["PropertyInfoMsg"]:
    db2 = MySQLdb.connect(host="localhost", user="user", passwd="pass", db="db")
    cursor2 = db2.cursor()
    sql = """UPDATE settings SET value=%s WHERE name=%s""" % (i["PropertyType"].lower(), i["PropertyValue"].lower())
    cursor2.execute(sql)
    db2.commit()
    cursor2.close()

2 个答案:

答案 0 :(得分:2)

我会这样查询:

cursor2.execute("UPDATE settings SET value = %s WHERE name = %s",(i["PropertyType"].lower(), i["PropertyValue"].lower()))

答案 1 :(得分:2)

你应该用单引号包装你的参数:

sql = """UPDATE settings SET value='%s' WHERE name='%s'""" % (i["PropertyType"].lower(), i["PropertyValue"].lower())

此外,您必须确认您的json数据是否正确。