0

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")
1
  • Reading this will give you some pointers. The python sqlite bindings don't have the features or power of the C API, but there's still stuff you can take away from it. Start with transactions, and also read up on the executemany() method. Commented Sep 18, 2018 at 2:45

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.