好的,有一个返回json格式的API,如下所示:
http://api.example.com/people/get
我正在尝试从网址import json, MySQLdb, urllib2, time
# database object
db = MySQLdb.connect("localhost","root","foo","bar")
x = db.cursor()
# API url
url = "http://api.example.com/people/get"
response = urllib2.urlopen(url)
jsonString = response.read()
toJson = json.loads(jsonString)
# result counts
total_count = toJson['count']
i = 1
pages = total_count / 100
while (i < pages):
_url = "http://api.example.com/people/get/page"+ str(i) +"&per_page=100"
_response = urllib2.urlopen(_url)
_jsonString = _response.read()
_toJson = json.loads(_jsonString)
user = _toJson['people']
for r in user:
user_id = r['id']
name = r['name']
full_name = r['full_name']
url = r['url']
try:
x.execute("""INSERT INTO repos (id, name, f_name, url)
VALUES ('%d', '%s', '%s', '%s') """, (user_id, name, full_name , url))
db.commit()
except:
db.rollback()
i += 1
db.close()
获取这些内容并将结果提交到数据库。为此,这是代码。
setContentView(R.layout.activity_main);
我不知道上面的脚本是否存在逻辑问题,因为它无效。没有数据提交给数据库。
答案 0 :(得分:0)
你吞咽异常......不要这样做。
你不知道.execute()
是否失败或为什么。
您可以打印出这样引发的异常:
except Exception as e:
print e