kma*_*mur 83 mysql django django-admin
如何使用manage.py和命令行从数据库中删除所有表?有没有办法用适当的参数执行manage.py,所以我可以从.NET应用程序执行它?
Man*_*dan 121
据我所知,没有管理命令可以删除所有表.如果您不介意攻击Python,您可以编写自己的自定义命令来执行此操作.您可能会发现该sqlclear
选项很有趣.文档说明./manage.py sqlclear
打印给定应用程序名称的DROP TABLE SQL语句.
更新:在这个答案下方无耻地挪用@Mike DeSimone的评论,给出一个完整的答案.
./manage.py sqlclear | ./manage.py dbshell
Run Code Online (Sandbox Code Playgroud)
从django 1.9开始,现在就是这样 ./manage.py sqlflush
Mik*_*one 34
如果您正在使用South软件包来处理数据库迁移(强烈推荐),那么您可以使用该./manage.py migrate appname zero
命令.
否则,我建议./manage.py dbshell
在标准输入的SQL命令中使用命令.
Cir*_*四事件 21
python manage.py migrate <app> zero
sqlclear
从1.9中删除.
发行说明提到它是由于迁移的引入:https://docs.djangoproject.com/en/1.9/releases/1.9/
遗憾的是,我找不到一种适用于所有应用程序的方法,也没有一种内置方法可以从管理员列出所有已安装的应用程序:如何在Django中列出所有已安装的应用程序和manage.py?
小智 7
简单(?)方式从python(在mysql上)做到这一点:
from django.db import connection
cursor = connection.cursor()
cursor.execute('show tables;')
parts = ('DROP TABLE IF EXISTS %s;' % table for (table,) in cursor.fetchall())
sql = 'SET FOREIGN_KEY_CHECKS = 0;\n' + '\n'.join(parts) + 'SET FOREIGN_KEY_CHECKS = 1;\n'
connection.cursor().execute(sql)
Run Code Online (Sandbox Code Playgroud)
这是我最终拼凑起来处理这个问题的一个 shell 脚本。希望它可以节省一些时间。
#!/bin/sh
drop() {
echo "Droping all tables prefixed with $1_."
echo
echo "show tables" | ./manage.py dbshell |
egrep "^$1_" | xargs -I "@@" echo "DROP TABLE @@;" |
./manage.py dbshell
echo "Tables dropped."
echo
}
cancel() {
echo "Cancelling Table Drop."
echo
}
if [ -z "$1" ]; then
echo "Please specify a table prefix to drop."
else
echo "Drop all tables with $1_ prefix?"
select choice in drop cancel;do
$choice $1
break
done
fi
Run Code Online (Sandbox Code Playgroud)
如果您想完全擦除数据库并同时重新同步它,您需要类似以下内容。我还结合在此命令中添加测试数据:
#!/usr/bin/env python
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings") # Replace with your app name.
from django.db import connection
from django.core.management import call_command
from django.conf import settings
# If you're using postgres you can't use django's sql stuff for some reason that I
# can't remember. It has to do with that autocommit thing I think.
# import psychodb2 as db
def recreateDb():
print("Wiping database")
dbinfo = settings.DATABASES['default']
# Postgres version
#conn = db.connect(host=dbinfo['HOST'], user=dbinfo['USER'],
# password=dbinfo['PASSWORD'], port=int(dbinfo['PORT'] or 5432))
#conn.autocommit = True
#cursor = conn.cursor()
#cursor.execute("DROP DATABASE " + dbinfo['NAME'])
#cursor.execute("CREATE DATABASE " + dbinfo['NAME'] + " WITH ENCODING 'UTF8'") # Default is UTF8, but can be changed so lets be sure.
# Mysql version:
print("Dropping and creating database " + dbinfo['NAME'])
cursor = connection.cursor()
cursor.execute("DROP DATABASE " + dbinfo["NAME"] + "; CREATE DATABASE " + dbinfo["NAME"] + "; USE " + dbinfo["NAME"] + ";")
print("Done")
if __name__ == "__main__":
recreateDb();
print("Syncing DB")
call_command('syncdb', interactive=False)
print("Adding test data")
addTestData() # ...
Run Code Online (Sandbox Code Playgroud)
能够这样做会很好,cursor.execute(call_command('sqlclear', 'main'))
但是call_command
将 SQL 打印到标准输出而不是将其作为字符串返回,而且我无法计算出sql_delete
代码......
归档时间: |
|
查看次数: |
96740 次 |
最近记录: |