python的Sqlite问题

2024-10-03 21:31:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须一次向Sqlite数据库(4个线程)添加大量记录,但数据库中只有4条记录。代码正在正确迭代。
代码段:

def inserter(filename):
    count = len(open(filename, 'rU').readlines())
    print str(count) + " " + filename
    for counti in range(count):
        conn = sqlite3.connect('passes.db')
        c = conn.cursor()
        m = hashlib.sha256()
        x = linecache.getline(filename, counti)
        print x + filename
        m.update(x)
        hashe = m.hexdigest()
        x = str(x)
        c.execute("INSERT INTO `passes`(`hashed`,`pass`) VALUES (?,?)", (hashe, x))
    conn.commit()
    conn.close()
    iter = ["aaa","aab","aac","aad"]
    for it in iter:
        p = multiprocessing.Process(target=inserter, args=(it,))
        p.start()
        p.join()
        print("process with id %s for file %s started" % (str(os.getpid()), it))

Tags: in数据库forcount记录itfilenameconn