Python 如何查看 django sqlite3 db 的数据库和架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42227778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:25:55  来源:igfitidea点击:

How to view database and schema of django sqlite3 db

pythondjangosqlite

提问by Shivkumar kondi

I am new to django framework.

我是 Django 框架的新手。

I tried to create a simple blog by following djangogirls tutorials.

我尝试按照 djangogirls 教程创建一个简单的博客。

Here by default we get sqlite3 as default database Engine

这里默认我们得到 sqlite3 作为默认数据库引擎

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

I tried some ORM queriesalso, Even performed some row sql queries

我也尝试了一些ORM 查询,甚至执行了一些行 sql 查询

At my django project I have this db.sqlite3file

在我的 django 项目中,我有这个db.sqlite3文件

blog  db.sqlite3  env  manage.py  mysite

My Question:How to knew the schema that django created in this db.sqlite3(I knew mysql where I can see details about each database and tables,so here I just want to know more things in sqlite)

我的问题:如何知道 django 在这个 db.sqlite3 中创建的模式(我知道 mysql,在那里我可以看到有关每个数据库和表的详细信息,所以在这里我只想了解更多关于 sqlite 的内容)

I have sqlite3 in my system and I tried .database command,but it just shows me

我的系统中有 sqlite3 并且我尝试了 .database 命令,但它只显示我

seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main 

回答by e4c5

Goto the folder where the database is and then

转到数据库所在的文件夹,然后

sqlite3  db.sqlite3

Then

然后

.tables

or .schema

或 .schema

depending on what you want. Instead of invoking sqlite3 directly you could do

取决于你想要什么。您可以不直接调用 sqlite3

 python manage.py dbshell 

and then type the sqlite commands.

然后输入sqlite命令。

If you are working with a legacy database you can generate Django models for that using the

如果您使用的是遗留数据库,则可以使用

 python manage.py inspectdb

please see https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-inspectdbfor additional info.

请参阅https://docs.djangoproject.com/en/3.0/ref/django-admin/#django-admin-inspectdb了解更多信息。

回答by Anish Kelkar

You can use the following command to get the sql script for the database created.

您可以使用以下命令获取创建的数据库的 sql 脚本。

python manage.py sqlmigrate app_label migration_name

python manage.py sqlmigrate app_label migration_name

回答by Pooja Khatri

You can view your db.sqlite file online.
sqlite Online

您可以在线查看您的 db.sqlite 文件。
sqlite在线

Just upload your file there and you can see your tables which are in your db.sqlite file.

只需将您的文件上传到那里,您就可以看到您的 db.sqlite 文件中的表格。

回答by dimButTries

I have been stumbling around for an hour aiming to replicate DESCRIBEtable inside the Django shell, and think I've cracked it. I hope this is of use to others.

我已经绊倒了一个小时,目的是DESCRIBE在 Django shell 中复制表,并认为我已经破解了它。我希望这对其他人有用。

In the Terminal - enter the following commands.

在终端中 - 输入以下命令。

python3 manage.py dbshell
.tables

Find the name of the table you are looking for, then run the following commands:

找到您要查找的表的名称,然后运行以下命令:

.header on
.mode column
pragma table_info('table you are looking for');

Do not forget the semicolon in the last instruction.

不要忘记最后一条指令中的分号。