Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

XML to MS SQL

import pyodbc
import xlrd
import xml.etree.ElementTree as ET

print("Connecting..")
# Establish a connection between Python and SQL Server
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=TEST;'
                      'Database=test;'
                      'Trusted_Connection=yes;')
print("DB Connected..")

# Get XMLFile
XMLFilePath = open('C:HelloWorld.xml')
x = etree.parse(XBRLFilePath)          # Updated Code line
with open("FileName", "wb") as f:      # Updated Code line
    f.write(etree.tostring(x))         # Updated Code line

# Create Table in DB
CreateTable = """
create table test.dbo.TEMP
(

 XBRLFile XML

)
"""

# execute create table
cursor = conn.cursor()
try:
    cursor.execute(CreateTable)
    conn.commit()
except pyodbc.ProgrammingError:
    pass
print("Table Created..")

InsertQuery = """
INSERT INTO test.dbo.TEMP (
    XBRLFile
) VALUES (?)"""

# Assign values from each row
values = etree.tostring(x) # Updated Code line

# Execute SQL Insert Query
cursor.execute(InsertQuery, values)

# Commit the transaction
conn.commit()

# Close the database connection
conn.close()
Comment

PREVIOUS NEXT
Code Example
Python :: text files to words generator 
Python :: Python - Cara Memisahkan String Berdasarkan Beberapa Delimiter 
Python :: geomertry 
Python :: how to search for element in list python 
Python :: online python debugger 
Python :: bouon arrondi tkinter 
Python :: concatenate the squares of numbers in python 
Python :: form is undefined flask 
Python :: pep8 E302 
Python :: ping all ip addresses in a network 
Python :: grandest staircase foobar 
Python :: Kinesis Client get_records response json 
Python :: featch detail of subscription in stripe api 
Python :: specificity formula python 
Python :: access value of posted object python 
Python :: Python Print Variable Using the + operator to join variables 
Python :: playlist discordpy 
Python :: =adaqtar 
Python :: Install pip and add virtual environment to the Python Kernel 
Python :: is assimilation part of digestive system 
Python :: print e 
Python :: Cget subassembly civid3d 
Python :: Multiple sub in single regex. 
Python :: Finding the Sum of a Symmetrical Sub-List 
Python :: make a coo_matrix 
Python :: Freqtrade - Informative Pairs 
Python :: python default summary statistics for all columns 
Python :: import baseestimator 
Python :: pandas series add prefix 
Python :: how to open any application in python 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =