0

I have created a sqlite database in python according to a tutorial:

from sqlalchemy import create_engine
engine = create_engine('sqlite:///DB.db') 

And next time I run the python script, it has old data in there.

How can I delete all rows, reinitialise or even delete the database (where is it located?)?

1
  • You can simply delete the Db.db file. It will be recreated the next time. Commented Feb 17, 2022 at 21:34

2 Answers 2

5

If you don't need the data to persist, you can use an in-memory sqlite database, using

engine = create_engine('sqlite://') 

If you still want the data to be accessible after you run your script, but each execution should just start anew, simply delete the DB.db file wherever the working directory is, at the start of your script:

import os
if os.path.exists('DB.db'):
   os.remove('DB.db')
Sign up to request clarification or add additional context in comments.

1 Comment

Such simple command, I could find nowhere across the globe. Thanks!
0

You can use the following code:

file_location = engine.url.database
# To stop using the file
engine.dispose()

# Drop file
os.remove(file_location)

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.