使用python将数据从xml文件填充到sqlite数据库 [英] populating data from xml file to a sqlite database using python
问题描述
我有一个有关解决问题的指导的问题.我有一个xml文件,我必须使用脚本语言Python将其填充到数据库系统中(无论它是sqlite还是mysql).
I have a question related to some guidances to solve a problem. I have with me an xml file, I have to populate it into a database system (whatever, it might be sqlite, mysql) using scripting language: Python.
有人对如何进行有任何想法吗?
Does anyone have any idea on how to proceed?
- 我需要进一步阅读哪些技术?
- 我必须安装哪些环境?
- 是否有关于同一主题的教程?
我已经尝试使用其他语言通过基于树的方法和sax方法来解析xml,但是从Python开始,我不知道从哪里开始.我已经知道如何设计所需的数据库.
I already tried to parse xml using both by tree-based and sax method in other language, but to start with Python, I don't know where to start. I already know how to design the database I need.
另一个问题,仅Python是否可以执行数据库ddl查询?
Another question, is Python alone possible of executing database ddl queries?
推荐答案
我建议您研究 ElementTree用于将XML文件解析到内存中(解析所有内容,然后将其全部发送到SQL DB,可能更容易,但是如果文件很大,元素树还允许增量操作)-这是标准的一部分将Python库作为模块 xml.etree .
I recommend you study on ElementTree for parsing your XML file into memory (parse it all, then emit it all to a SQL DB, is probably easier, but element-tree also allows incremental operation if your file is huge) -- it's part of the standard Python library as module xml.etree.
我建议 sqlite3 (也在标准中Python库)作为选择的关系数据库(如果您有选择的话),再次因为它方便易用-在
I recommend sqlite3 (also in the standard Python library) as the relational DB of choice (if you have a choice), again because it's handy and easy -- the underlying SQLite embedded relational DB is also well documented at its own site. If you need a general tutorial on how Python likes to interface to relational DBs (the "DB-API"), there's a nice one here.
一旦您完全了解etree和sqlite3,并且不必为其中任何一个安装任何(这是它们的魅力所在;-)-您基本上已经设置好了.(当然,可以安装SQLite本身(命令行和/或GUI工具),以便您查看数据库文件并进行调整,就像 XMLmind 来查看和/或调整您的XML,但根本不需要 这两种工具,根本与使用Python而不是与其他语言进行XML解析和SQLite编写有关;-).
Once you fully understand etree and sqlite3 -- and you don't necessarily need to install anything for either (which is part of their charms;-) -- you're basically all set. (Of course an installation of SQLite itself (commandline and/or GUI tools) so you can look at your DB files and tweak them may be nice, as may graphical XML editors such as oXygen or XMLmindto look at and/or tweak your XML, but neither kind of tool is at all needed, not at all related to using Python rather than other languages for the XML parsing and SQLite writing;-).
是的,如果选择使用其他代码,则可以很好地从 sqlite3
(以及任何其他符合DB-API的Python模块)进行 CREATE TABLE
和其他DDL查询关系数据库;-).
And yes, you can perfectly well do CREATE TABLE
and other DDL queries from sqlite3
(and any other DB-API compliant Python module, if you choose to use other relational DBs;-).
这篇关于使用python将数据从xml文件填充到sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!