[英]How can I insert the rows I received from the API into mysql
how i can insert my table mysql result this code?我怎样才能插入我的表 mysql 结果这个代码?
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
user_json = json.loads(response.text)
for i in user_json:
db_ins = pymysql.connect(host="localhost", user="root", password="password", db="vmremover")
cursor_insert = db_ins.cursor()
sql_insert = "INSERT INTO `users` (`id`, `username` ................
This is while wrong, i know :) how me write correct?
for i in user_json:
sql_insert = "INSERT INTO `users` (`id`, `username` ...)
this i need help:)这我需要帮助:)
I think the problem you are experiencing is in writing right query so that with every passing loop you can add the desired data to the mysql database.我认为您遇到的问题是编写正确的查询,以便每次通过循环时,您都可以将所需的数据添加到 mysql 数据库中。 It can be done as follows:可以按如下方式完成:
sql_insert="INSERT INTO table_name (id, username ...) VALUES (%s, %s, ...)"
sql_insert.execute(sql_insert, (i['id'], i['name']...))
In above code, in the second statement the values (i['id']...) replace the %s inside the first line of code.在上面的代码中,在第二个语句中,值 (i['id']...) 替换了第一行代码中的 %s。 Hence, it should write the intended data into the table for every iteration.因此,它应该为每次迭代将预期数据写入表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.