You should really consider removing "select * from" and replacing it with a "select" that specifically states what fields it's selecting and in which order. The same applies to "insert into table". This way your code will continue working even if table layout is altered. Since you're doing a "describe" already, it's easy enough to pull field names from it. Once you have the names, do a proper "select" and also store field names in the file that you're exporting, perhaps on the very first line, so that the application on the other end will know how to construct the "insert". Getting the right number of "?" for placeholders is also trivial in this case.