简体   繁体   English

使用 Python 将 XML 文件存储到 MS SQL DB 中

[英]Store XML File into MS SQL DB using Python

My MSSQL DB table contains following structure:我的 MSSQL DB 表包含以下结构:

create table TEMP
(
  MyXMLFile XML

)

Using Python, I a trying to load locally stored.XML file into MS SQL DB (No XML Parsing Required)使用Python,我试图在本地加载.Z3501BB093D33810B671059B9B9B9B9CFED3B9B9CFED331B.NOIRDIE

Following is Python code:以下是 Python 代码:

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')

# 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 = (XMLFilePath)

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

# Commit the transaction
conn.commit()

# Close the database connection
conn.close()

But the code is storing the XML path in MYXMLFile column and not the XML file.但是代码将 XML 路径存储在 MYXMLFile 列中,而不是 XML 文件中。 I referred lxml library and other tutorials.我提到lxml库和其他教程。 But, I did not encountered straight forward approach to store file.但是,我没有遇到直接存储文件的方法。

Please can anyone help me with it.请任何人都可以帮助我。 I have just started working on Python.我刚刚开始研究 Python。

Here, is solution to load.XML file directly into MS SQL SB using Python.在这里,是使用 Python 将文件直接加载到 MS SQL SB 中的解决方案。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM