简体   繁体   中英

SQLALCHEMY delete all rows in existing database in python

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM