python 找bug

120 阅读1分钟
connection = pymysql.connect(
    host='localhost',
    user='root',
    passwd="yang",
    db='proyectoBD',
    port=3306)


curs = connection.cursor()
curs.execute("select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='proyectoBD';")
result = curs.fetchall()
result = list(result)
doc = xml.dom.minidom.Document()
root = doc.createElement('root')
doc.appendChild(root)

for table in result:
    table = str(table).split(",")[0]
    table_name = table[2:len(table) - 1]
    tables = doc.createElement(table_name)
    root.appendChild(tables)

    curs.execute("select * from " + table_name + " limit 1;")
    table_content = curs.fetchall()
    table_content = list(table_content)
    curs.execute("select COLUMN_NAME from information_schema.COLUMNS where table_name = "+"'" +table_name+"';")
    columns = curs.fetchall()
    attributes = list(columns)
    row = 0

    while (row < len(table_content)):
        element = doc.createElement("Element")
        tables.appendChild(element)
        col = 0
        while (col < len(attributes)):

            attribute = str(attributes[col]).split("'")[1]
            att = doc.createElement(attribute)
            element.appendChild(att)
            text = table_content[row][col]
            att.appendChild(doc.createTextNode(str(text)))
            col = col + 1
        row += 1


f = open('/Users/yangyangli/Desktop/Ingenieria/bdBiologico/generaXML/bd_xml.xml','w')
doc.writexml(f, indent='\t', addindent='\t', newl='\n')
f.close()
connection.close()

###错误是: Traceback (most recent call last): text = table_content[row][col] IndexError: tuple index out of range

我正在用这个脚本想把数据库的内容写到xml格式, 可是通过debugger发现每一步都是好的,不知道为什么text = table_content[row][col] 这一行总是报错, 哪位朋友能帮助我一下,谢谢了!!