【发布时间】:2021-04-17 09:42:26
【问题描述】:
我已经编写了一个脚本来更新我的 mysql 数据库中的库存,但是我只从更新的 xml 中获取了最后一条记录,我正在通过一个隧道寻找正确的知道并且喜欢有一双新的眼睛看着它。
import requests
import xml.etree.ElementTree as ET
from lxml import etree
from getpass import getpass
from mysql.connector import connect, Error
r = requests.get('http://api.edc.nl/xml/eg_xml_feed_stock.xml')
root = ET.fromstring(r.content)
for x in root.iter('product'):
id = x.find('productid').text
qty = x.find('qty').text
try:
with connect(
host="my host",
user=input("Enter username: "),
database="my database",
password=getpass("Enter password: "),
) as connection:
query = "UPDATE `ps_stock_available` SET `quantity` = " + \
qty + " WHERE `id_product` = " + id + ";"
with connection.cursor() as cursor:
cursor.execute(query)
connection.commit()
#result = cursor.fetchall()
# for row in result:
print(query)
except Error as e:
print(e)
【问题讨论】:
-
不要使用字符串连接替代查询,使用占位符和参数。
-
是的,每天都在学习,谢谢。但这与最后的记录有关吗?我认为占位符和参数正在使用这个 %s?
标签: python xml-parsing mysql-python