简体   繁体   English

如何将我从 API 收到的行插入 mysql

[英]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.

相关问题 如何使用python更快地将行插入mysql表? - How can I insert rows into mysql table faster using python? 如何将我从远程MySQL数据库选择的一些行插入本地MySQL数据库 - how do I insert some rows that I select from remote MySQL database to my local MySQL database 如何在MYSQL中插入多个值? - How can I insert multiple values to the MYSQL? 如何将数据插入到 MySQL 数据库中? - How can I insert data into a MySQL database? 如何在peewee中使用INSERT REPLACE插入多行? - How can I insert multiple rows with INSERT REPLACE in peewee? 如何使用SQLAlchemy和Postgres RETURNING从一个表中删除行并插入到另一个表中? - How can I delete rows from one table and insert into another using SQLAlchemy and Postgres RETURNING? 如何使用 python 从树莓派向 Oracle SQL 数据库中插入行? - How can I insert rows into Oracle SQL databse from a raspberry pi using python? 如何在 Pandas DataFrame 中的给定位置插入多行? - How can I insert several rows at given position in pandas DataFrame? 如何使用pymysql通过存储过程将pyodbc mssql查询返回的列表插入到mysql中 - How can I insert a list returned from pyodbc mssql query into mysql through stored procedure using pymysql 如何使用Python将从MySQL表中选择的行列成功插入到目标MSSQL表中? - How can I successfully insert row columns selected from MySQL table into a destination MSSQL table using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM