python fdb将大量数据从数据库保存到文件

python fdb将大量数据从数据库保存到文件,python,fdb,Python,Fdb,我有这个剧本 SELECT = """ select coalesce (p.ID,'') as id, coalesce (p.name,'') as name, from TABLE as p """ self.cur.execute(SELECT) for row in self.cur.iterma

我有这个剧本

SELECT = """
            select 
              coalesce (p.ID,'') as id, 
              coalesce (p.name,'') as name, 
            from TABLE as p  
         """
self.cur.execute(SELECT)
for row in self.cur.itermap():         
    id = '%(id)s' % row
    name = '%(name)s' % row

    xml +="  <item>\n"      
    xml +="    <id>"         + id + "</id>\n"
    xml +="    <name>"    + name + "</name>\n"    
    xml +="  </item>\n\n"
    
#save xml to file here
f = open...

  

哇,用代码测试之后

result = float(id)/2000
if result == int(result):
  xml_whole += xml
  xml = ""
我的脚本快了50倍!
我想知道为什么python在xml+=

您正在做很多不必要的工作,但是,如果删除xml变量,您就不会像以前那样写入相同的数据

为什么不直接编写XML呢?您还可以避免这两种合并,并在Python中编写该检查,如果ID为null,则生成ID,等等

SELECT = """
            select 
              coalesce (p.ID,'') as id, 
              coalesce (p.name,'') as name, 
            from TABLE as p  
         """
self.cur.execute(SELECT)

# Open XML file
f = open("file.xml", ...)
f.write("<?xml version... (what encoding?)

for row in self.cur.itermap():
    f.write("<item>\n    <id>%(id)s</id>\n    <name>%(name)s</name>\n</item>\n"

# Other f.writes() if necessary
f.close()

如果不使用XML输出会发生什么情况?看起来要快一点。
SELECT = """
            select 
              coalesce (p.ID,'') as id, 
              coalesce (p.name,'') as name, 
            from TABLE as p  
         """
self.cur.execute(SELECT)

# Open XML file
f = open("file.xml", ...)
f.write("<?xml version... (what encoding?)

for row in self.cur.itermap():
    f.write("<item>\n    <id>%(id)s</id>\n    <name>%(name)s</name>\n</item>\n"

# Other f.writes() if necessary
f.close()