将数据从xml文件导入两个表,并在MySQL数据库中使用外键

时间:2016-09-21 16:26:08

标签: python mysql

我需要将以下格式的文件加载到MySQL数据库中。

<item value="{$\emptyset $}">
   <subitem value="(empty language)"></subitem>
   <subitem value="(empty set)"></subitem>
</item>
<item value="{$\subseteq$ (subset)}">
</item>
<item value="{$\subset$ (proper subset)}">
</item>
<item value="{$:$ (such that)}">
</item>
<item value="{$\cap$ (set intersection)}">
</item>
<item value="{$\cup$ (set union)}">
</item>
<item value="{$-$ (set difference)}">
</item>
<item value="{$\left | \mskip \medmuskip \right |$}">
   <subitem value="(flow value)"></subitem>
   <subitem value="(length of a string)"></subitem>
   <subitem value="(set cardinality)"></subitem>
</item>

我认为在数据库中它应该由两个表表示,Subitem表应该包含外键:

项目&lt; - Subitem

我想用python做这件事。是否可以仅使用MySQL指令来完成它,或者最好在python中加载xml文件,手动创建两个表,然后将所有条目插入到我想要的表中?

1 个答案:

答案 0 :(得分:0)

我能够通过使用python读取xml然后将其插入MySQL数据库来实现。首先需要安装所需的软件:

sudo apt install mysql-server
sudo apt-get install python-mysqldb

然后这个py文件将完成这项工作:

import xml.etree.ElementTree
import MySQLdb

try:
    db = MySQLdb.connect(host="localhost",
                         user="root",
                         passwd="!") 

    cur = db.cursor()

    cur.execute("DROP DATABASE IF EXISTS i2a")
    cur.execute("CREATE DATABASE i2a")
    cur.execute("USE i2a")
    print "Created database"

    cur.execute("""
        CREATE TABLE Item (
            id INT NOT NULL AUTO_INCREMENT,
            `value` VARCHAR(255) NOT NULL,
            PRIMARY KEY (`id`)
        )
        DEFAULT CHARACTER SET = utf8
        COLLATE = utf8_bin""")
    print "Created Item table"

    cur.execute("""
        CREATE TABLE Subitem (
            id INT NOT NULL AUTO_INCREMENT,
            item_id INT NOT NULL,
            `value` VARCHAR(255) NOT NULL,
            PRIMARY KEY (`id`),
            FOREIGN KEY (item_id) REFERENCES Item(id) ON DELETE RESTRICT
        )
        DEFAULT CHARACTER SET = utf8
        COLLATE = utf8_bin""")
    print "Created Subitem table"

    e = xml.etree.ElementTree.parse('index.xml').getroot()

    for item in e.findall('item'):
        cur.execute("INSERT INTO Item (value) VALUES (%s)", [item.get('value')])
        for subitem in item:
            cur.execute("INSERT INTO Subitem (item_id, value) VALUES (%s, %s)", (db.insert_id(), subitem.get('value')))
    print "All data are there!"
except Exception, e:
     print str(e)