Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从json查询mysql来自angularjs-Python_Python_Json_Angularjs - Fatal编程技术网

从json查询mysql来自angularjs-Python

从json查询mysql来自angularjs-Python,python,json,angularjs,Python,Json,Angularjs,大家好,我已经得到了json数据,这是我从angularjs得到的json数据,有人能帮我吗?。我只是被它卡住了。多谢各位 {u'isChecked': {u'49871': False, u'49870': True, u'113634': False}} 然后,在我的python中,我想在json数据中找到id时更新mysql updatetable = """UPDATE table_x SET value = '1

大家好,我已经得到了json数据,这是我从angularjs得到的json数据,有人能帮我吗?。我只是被它卡住了。多谢各位

{u'isChecked': {u'49871': False, u'49870': True, u'113634': False}}
然后,在我的python中,我想在json数据中找到id时更新mysql

   updatetable = """UPDATE table_x
                                 SET value = '1'
                                """
        db.session.execute(updatetable)
        db.session.commit()
现在这是我的更新代码,我想把它连接到我的json数据

   updatetable = """UPDATE table_x
                                 SET value = '1'
                                """
        db.session.execute(updatetable)
        db.session.commit()
这里有一个解决方案

#!/usr/bin/env python

import platform
import sys
import urllib2
import simplejson as json

def update_table(id):
    sqlUpdateStr = "UPDATE table_x SET value = '1' where id="+id
    print "Executing update: " + sqlUpdateStr

def test_parse_json():
    print "Loading json ..."
    req = urllib2.Request("http://localhost/example.json")
    opener = urllib2.build_opener()
    f = opener.open(req)
    # json.load() will deserialize your JSON document and return a Python object.
    data = json.load(f)

    print data['isChecked']
    print ""

    for id in data['isChecked']:
        id_val = str2bool(data['isChecked'][id])
        if id_val == True:
            print "Found id for update: " + id
            update_table(id)
        else:
            print "Ignoring record with id=" + id

def str2bool(v):
    return v.lower() in ("yes", "true", "t", "1")

def main():
    test_parse_json()

    return


if __name__ == '__main__':
    main()
example.json的内容是:

{
    "isChecked":{
        "49870":"true",
        "49871":"false",
        "113634":"false"
    }
}

这是完全不清楚的。那个字典和SQL有什么关系?您想如何连接它们?你哪里有问题?字典来自json,是我想在mysql中更新的id列表。如果id是在json中找到的,并且是真的,那么我想在我的mysql表中将我的值更新为1。好的,到目前为止您有什么发现?所以在您提供的示例中,您应该更新id=49870的记录:update table_x SET value='1',其中id=49870;是吗?hi@gandra404是的,这就是我想要的