Merajul Islam Sami
Merajul Islam Sami

Reputation: 123

slow input on database SQlite3 python3

I am trying to save username on local sqlite3 db using python. I am really new. My Table has 10000 rows and just a column of username but it is taking more than half an hour to inset all 10k values. What is I am doing wrong ? Is there any clue ? My code

 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")

Upvotes: 0

Views: 36

Answers (1)

CL.
CL.

Reputation: 180020

The inserts are not slow; all the commit() calls are slow.

Remove them, and do the commit() only when you're actually finished.

Upvotes: 1

Related Questions