I am new to Django and my current task is to upload a xml file with 16 fields and more than 60000 rows to a database in PostgreSQL. I used Django to connect to the Database and was able to create a table in the database.
I also used XML Etree to parse the xml file. I am having trouble storing the data in the table that I created in the sql database.
This is the code that I used to parse:
import xml.etree.ElementTree as ET
def saveXML2db():
my_file = "C:/Users/Adithyas/myproject/scripts/supplier_lookup.xml"
tree = ET.parse(my_file)
root = tree.getroot()
cols = ["organization", "code", "name"]
rows = []
for i in root:
organization = i.find("organization").text
code = i.find("code").text
name = i.find("name").text
x = rows.append([organization, code, name])
data = """INSERT INTO records(organization,code,name) VALUES(%s,%s,%s)"""
x.save()
saveXML2db()
the code runs without any error, but I am unable to store the data into the table in the SQL database.
2
Answers
So I figured out the answer to my question and I wish to share this with you guys. This is how I imported a xml file to PostgreSQL database using Django ORM:
First, I created a virtual environment to work with: open command prompt in the folder you wish to run the project
our virtual environment is ready to use then,
now both our project and app is created and ready to use
code . #to open Visual code
now go to settings.py in 'projectq' and add 'myapp' to INSTALLED_APPS:
now to connect our project to PostgreSQL database we have to make some changes in the DATABASES in settings.py as well:
change dbsqlite to the name of the database that you are using, add name of your Database, username and password
now the connection is established. we move on to the next step
go to models.py to create our table in PostgreSQL to store our xml data:
If your data has null values it's best to add null = True, to avoid errors
now the table we created should appear on the PostgreSQL database
next step is to parse our xml file and to import it to the table we created. For that we will use Django ORM queries
open terminal in our visual code in models.py activate virtual environment again
to use ORM query:
now add these codes to the interactive console:
That's It. The data should be loaded into the database now. Hope this helps.
Have you checked any python/PostgreSQL examples? Your code should have something like this (untested):