【问题标题】:How can I insert the rows I received from the API into mysql如何将我从 API 收到的行插入 mysql
【发布时间】:2021-05-19 12:04:47
【问题描述】:

我怎样才能插入我的表 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` ...)

这个我需要帮助:)

【问题讨论】:

标签: python mysql pymysql


【解决方案1】:

我认为您遇到的问题是编写正确的查询,以便在每次通过循环时,您都可以将所需的数据添加到 mysql 数据库中。可以这样做:

sql_insert="INSERT INTO table_name (id, username ...) VALUES (%s, %s, ...)"
sql_insert.execute(sql_insert, (i['id'], i['name']...))

在上面的代码中,在第二个语句中,值 (i['id'] ...) 替换了第一行代码中的 %s。 因此,它应该在每次迭代时将预期数据写入表中。

【讨论】:

  • @Sshoaib-wani for i in user_json: print(i['id'], i['mail']) error TypeError: string indices must be integers
  • print(user_json['result'][0]['mail']) print(user_json['result'][1]['mail']) 这是正确的,我如何插入sql with while @Shoaib_wani
  • 在上面粘贴完整的代码,我可以帮你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 2015-01-05
  • 2014-05-23
  • 2021-02-13
  • 1970-01-01
  • 2018-11-02
相关资源
最近更新 更多