When I try to put in a SQL database the subelement attribute and text, I get this:
Failed inserting record into python_users table 1054 (42S22): Unknown column 'a' in 'field list'".
SQL itself it's set with a table named "valoare" and 2 areas for values("moneda" & "flux")
for child in root:
for element in child:
for subelement in element:
a = subelement.attrib["currency"]
b = subelement.text
connection = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin",
database="python",
)
sql_insert_query = """ INSERT INTO valoare
(moneda, flux) VALUES (a, b)"""
cursor = connection.cursor()
result = cursor.execute(sql_insert_query)
connection.commit()
The actual error is caused by not using placeholders like you're supposed to.
Also, you really don't want to be reconnecting to the database like that for each element. In addition, you can only commit when everything's done: