我有一个可以进行一些转换的工作程序,但我很担心,如果数据库太大会发生什么。
I'll make you clear like, if the below program bombs in the middle how would i get the program recover o get it working from the specified line of code.
执行一段代码后,进程肯定会被杀死。
程序是否能够从出错的地方继续返回,或者从被杀死的位置继续返回。
import sqlite3 as sqlite
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('removespecial.ini')
con = sqlite.connect('listofcomp.sqlite')
cur = con.cursor()
def transremovechars():
char_cfg = open('specialchars.txt', 'r') #Reads all the special chars to be removed from specialchars.txt#
special_chars = char_cfg.readline()
char_cfg.close()
cur.execute('select * from originallist')
for row in cur: #Applies transformation to remove chars for each row in a loop#
company = row[0]
for bad_char in special_chars:
company = company.replace(bad_char, '')
cur.execute('Create table transform1 (Names Varchar, Transformtype Varchar')
cur.execute('Insert into transform1 (Names)', company)
def transtolower():
cur.execute('select * from transform1') #Transformation to convert all the namesto lower cases#
for row in cur:
company = row[0]
company = company.lower()
cur.execute('Create table transform2 (Names Varchar, Transformtype Varchar') #Creates another table named transform2#
cur.execute('Insert into transform2 (Names)', company) #Copies all the lower cased names to transform2#
if __name__=="__main__":
transremovechars()
transtolower()
最佳答案
if the below program bombs in the middle how would i get the program recover o get it working from the specified line of code
你不能。
您的代码非常神秘,因为除了第一个插入之外,创建表在每次插入之前都会收到错误。
但是,如果您想要从一个旧表到一个新表进行一系列插入操作, 并且您担心它可能无法正确完成,您有两种选择来维护所需的状态信息。
唯一键。
批处理。
查询。
唯一键。
如果每行都有唯一的键,则某些插入操作会出现错误,因为该行是重复的。
如果程序“爆炸”,您只需重新启动即可。您会得到很多重复项(如您所料)。这并不是低效的。
批处理。
我们使用的另一种技术是查询旧表中的所有行,并包含一个每 1000 行递增的“批”号。 batch_number = row_count//1000
。
您创建一个编号为 -1
的“批号”文件。
您的程序启动,它会读取批处理号。这是最后一批完成的。
您读取源数据,直到找到批号>最后一个完成的批号。
然后,您可以执行批处理中的所有插入操作。
当批处理号更改时,进行提交,并将批处理号写入文件。这样您就可以重新启动任何批处理。
在“炸弹”后重新启动时,您可能会从部分批处理中获得一些重复项(这是您所期望的)。这并不是低效的。
查询。
您可以在每次插入之前查询该行是否存在。这是低效的。
如果您没有唯一键,则必须执行复杂的查询来查看该行是否是由程序的先前运行创建的。
关于python - 我的 python 程序是否有任何特定方法可以从被杀死的行继续执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9209456/