sqlite / python3的优化技巧

时间:2018-06-08 23:12:49

标签: python-3.x sqlite

我是一个sqlite3 db:

sqlite> .schema
CREATE TABLE "wsenders" ("name" primary key not null unique, "status" text);

我希望处理一些行,并将这些行标记为"已处理"在数据库中。

sqlite> create temporary table xyz as select name from wsenders where status not like "processed" limit 10;
sqlite> update or ignore wsenders set status = "processed" where name in (select name from xyz) ;
sqlite> select name from xyz;

将10个项目复制到临时表,将这10个项目标记为"已处理"在主表中,从临时表中选择项并断开连接,继续对这些结果进行操作。当sqlite3断开连接时,临时表将被删除。

如何在Python中有效地执行此操作?我期待使用executioncript,转而不返回任何结果来获取。我最终将第一个结果写入列表,然后迭代每个项目以标记如下。然而,这似乎非常低效,并认为我应该停下来并要求建议。

def extract_senders(span,db):
    script = """select * from wsenders limit 5;"""
    try:
        connect = sqlite3.connect(db)
        cursor = connect.cursor()
        #cursor.executescript(script)
        #cursor.execute("""create temporary table xyz as select name from wsenders where status not like "processed" limit 10;select name from xyz limit 3""")
        cursor.execute("""select name from wsenders where status not like "processed" limit {span}""".format(span=span))
        results = cursor.fetchall()
        cursor.close()
        if results:
            for i in results:
                run_this = "update or ignore wsenders set status = '{stat}' where name = '{name}'".format(stat = "processed", name =i[0])
                runq_silent(run_this,db)
        <snip>

有什么更好的方法可以做到这一点?

0 个答案:

没有答案