Python 3.x 数据库SQlite3 python3的输入速度慢
我正在尝试使用python在本地SQLite3DB上保存用户名。我是新来的。我的表有10000行,只有一列用户名,但插入所有10k值需要半个多小时。我做错了什么?有什么线索吗?我的代码Python 3.x 数据库SQlite3 python3的输入速度慢,python-3.x,sqlite,Python 3.x,Sqlite,我正在尝试使用python在本地SQLite3DB上保存用户名。我是新来的。我的表有10000行,只有一列用户名,但插入所有10k值需要半个多小时。我做错了什么?有什么线索吗?我的代码 def create_table(channel_username): c.execute("CREATE TABLE IF NOT EXISTS {}(user_name UNIQUE)".format(channel_username)) conn.commit() def insert
def create_table(channel_username):
c.execute("CREATE TABLE IF NOT EXISTS {}(user_name UNIQUE)".format(channel_username))
conn.commit()
def insert_username(channel_username,user_name):
c.execute("INSERT OR IGNORE INTO {} VALUES(?)".format(channel_username),[user_name])
conn.commit()
create_table(channel_username)
x= client.get_participants(channel_username, aggressive=True)
z=[]
for user in x:
z.append(user.username)
fl = [i for i in z if i is not None]
fl.sort()
for user_name in fl:
insert_username(channel_username,user_name)
print("DBfached successful")
插入不慢;所有的
commit()
调用都很慢
删除它们,并仅在实际完成时执行commit()
。阅读会给您一些提示。python-sqlite绑定没有C-API的特性或功能,但是仍然有一些东西可以从中获得。从事务开始,同时阅读executemany()
方法。