How to store XML data into a MySQL database using Python? Last Updated : 27 Mar, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we are going to store XML data into the MySQL database using python through XAMPP server. So we are taking student XML data and storing the values into the database. RequirementsXAMPP server: It is a cross-platform web server used to develop and test programs on a local server. It is developed and managed by Apache Friends and is open-source. It has an Apache HTTP Server, MariaDB, and interpreter for 11 different programming languages like Perl and PHP. XAMPP Stands for cross-platform, Apache, MySQL, PHP, and Perl. It can be easily installed from here.MySQL connector: MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Python Database API Specification v2.0 (PEP 249). It uses the Python standard library and has no dependencies. It can be installed using the below command:pip install mysql.connectorApproachStart XAMPP serverCreate XML file. XML Structure: <root> <child> <subchild>.....</subchild> </child> </root> We are going to use the XML module. xml.etree.ElementTree This is an Element Tree XML API that is used to implement a simple and efficient API for parsing and creating XML data. So we need to import this module. Syntax: import xml.etree.ElementTree The XML file to be created is names vignan.xml. XML <?xml version="1.0"?> <studentdata> <student> <name>Sravan Kumar</name> <id>7058</id> <department>IT</department> </student> <student> <name>Meghana</name> <id>7034</id> <department>IT</department> </student> <student> <name>Pranathi</name> <id>7046</id> <department>EEE</department> </student> <student> <name>Durga</name> <id>7078</id> <department>Mech</department> </student> <student> <name>Ishitha</name> <id>7093</id> <department>MBA</department> </student> </studentdata> For Python code:Create a python file names a.py.Import required module.Establish connection.Read XML file.Retrieve data from the XML and insert it into a table in the database.Display message on successful insertion of data. Python # import xml element tree import xml.etree.ElementTree as ET # import mysql connector import mysql.connector # give the connection parameters # user name is root # password is empty # server is localhost # database name is database conn = mysql.connector.connect(user='root', password='', host='localhost', database='database') # reading xml file , file name is vignan.xml tree = ET.parse('vignan.xml') # in our xml file student is the root for all # student data. data2 = tree.findall('student') # retrieving the data and insert into table # i value for xml data #j value printing number of # values that are stored for i, j in zip(data2, range(1, 6)): name = i.find('name').text id = i.find('id').text department = i.find('department').text # sql query to insert data into database data = """INSERT INTO vignan(name,id,department) VALUES(%s,%s,%s)""" # creating the cursor object c = conn.cursor() # executing cursor object c.execute(data, (name, id, department)) conn.commit() print("vignan student No-", j, " stored successfully") Output: Save both filesVerify the content of the table in which the values were recently inserted. Comment More infoAdvertise with us Next Article How to visualize data from MySQL database by using Matplotlib in Python ? S sravankumar_171fa07058 Follow Improve Article Tags : Python Python-mySQL Practice Tags : python Similar Reads How to visualize data from MySQL database by using Matplotlib in Python ? Prerequisites: Matplotlib in Python, MySQL While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python. MySQL Connector module of Py 5 min read How to Copy a Table in MySQL Using Python? In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module. 3 min read How to Copy a Table Definition in MySQL Using Python? Python requires an interface to access a database server. Python supports a wide range of interfaces to interact with various databases. To communicate with a MySQL database, MySQL Connector Python module, an API written purely in Python, is used. This module is self-sufficient meaning that it does 6 min read Extract Data from Database using MySQL-Connector and XAMPP in Python Prerequisites: MySQL-Connector, XAMPP Installation A connector is employed when we have to use MySQL with other programming languages. The work of mysql-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the M 2 min read How to Import a CSV file into a SQLite database Table using Python? In this article, we are going to discuss how to import a CSV file content into an SQLite database table using Python. Approach:At first, we import csv module (to work with csv file) and sqlite3 module (to populate the database table).Then we connect to our geeks database using the sqlite3.connect() 3 min read Export Data From Mysql to Excel Sheet Using Python We are given a MySQL database and our task is to export the data into an excel sheet using Python. In this article, we will see how to export data from MySQL to Excel Sheets using Python. Export Data From Mysql to Excel Sheet Using PythonBelow are some of the ways by which we can export data from My 3 min read Like