How to view database and schema of django sqlite3 db
Asked Answered
C

6

61

I'm new to django framework.

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

Here by default, we get sqlite3 as default database Engine:

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

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

At my django project, I have this db.sqlite3 file:

blog  db.sqlite3  env  manage.py  mysite

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

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

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

                                                                  
Casing answered 14/2, 2017 at 13:44 Comment(2)
Did you try .schema?Immutable
Yes it shows blank. tried .help and all commands alsoCasing
A
88

Goto the folder where the database is and then

sqlite3  db.sqlite3

Then

.tables

or .schema

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

 python manage.py dbshell 

and then type the sqlite commands.

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-inspectdb for additional info.

But please do yourself a favour and get a GUI database client. Life is much easier when you have one.

Allseed answered 14/2, 2017 at 13:47 Comment(3)
Well yes all this assumes that sqlite3 is available on your systemAllseed
sqlite3 installs with django installation.Journeyman
+1 for "please do yourself a favour and get a GUI database client. Life is much easier when you have one." Preach.Vitia
A
10

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

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.

Ashling answered 27/3, 2020 at 11:46 Comment(1)
and yes, forgot the semicolon at the end of the last instruction ;) Why is this line not blinking?Fisc
H
8

First, run the command below:

python manage.py dbshell

To access sqlite3 command-line client as shown below:

enter image description here

Then, run the command below:

.table

Or run the command below:

.tables

To show all the tables in SQLite as shown below:

enter image description here

Then, run the command below:

.schema --indent store_product

To show the schema of "store_product" table as shown below:

enter image description here

Hirschfeld answered 21/8, 2022 at 2:7 Comment(0)
V
4

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

python manage.py sqlmigrate app_label migration_name

Virchow answered 23/12, 2017 at 10:21 Comment(0)
A
1

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

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

Androgynous answered 12/10, 2019 at 9:41 Comment(1)
My file not openingBerberidaceous
R
1

Just wanted to add that someone can find the migrations under my_project_name>my_app_name>migrations and usually you pick the one with the biggest number.

Then the command should be like: python manage.py sqlmigrate my_app_name number

Rinse answered 17/12, 2021 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.