简体   繁体   English

SQLALCHEMY 删除 python 中现有数据库中的所有行

[英]SQLALCHEMY delete all rows in existing database in python

I have created a sqlite database in python according to a tutorial:我按照教程在python创建了一个sqlite的数据库:

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.下次我运行 python 脚本时,它里面有旧数据。

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如果不需要数据持久化,可以使用内存中的 sqlite 数据库,使用

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:如果您仍然希望在运行脚本后可以访问数据,但每次执行都应该重新开始,只需删除工作目录所在的 DB.db 文件,在脚本的开头:

import os
if os.path.exists('DB.db'):
   os.remove('DB.db')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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