Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view database and schema of django sqlite3 db

I am 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 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)

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

seq  name             file                                                       ---  ---------------  ---------------------------------------------------------- 0    main  
like image 778
Shivkumar kondi Avatar asked Feb 14 '17 13:02

Shivkumar kondi


People also ask

What is the Django command to view a database schema of an existing database?

Django inspectdb command Django provides a utility to auto-generate models from an existing database via inspectdb command. The output file will be saved to your current directory.

How do I view tables in SQLite Django?

If you're interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MariaDB, MySQL), .tables (SQLite), or SELECT TABLE_NAME FROM USER_TABLES; (Oracle) to display the tables Django created.


2 Answers

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.

like image 199
e4c5 Avatar answered Sep 29 '22 20:09

e4c5


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.

like image 32
dimButTries Avatar answered Sep 29 '22 22:09

dimButTries