Skip to content Skip to sidebar Skip to footer

I Try To Put Some Informations From A Xml File To A Sql Database Using Python

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'

Solution 1:

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:

connection = mysql.connector.connect(
    host="localhost", user="root", passwd="admin", database="python"
)
cursor = connection.cursor()

for child in root:
    for element in child:
        for subelement in element:
            a = subelement.attrib["currency"]
            b = subelement.text
            result = cursor.execute(
                "INSERT INTO valoare (moneda, flux) VALUES (%s, %s)", (a, b)
            )

connection.commit()

Post a Comment for "I Try To Put Some Informations From A Xml File To A Sql Database Using Python"