python sqlite use in terminal -django

2019-03-11 13:02发布

问题:

How can I see my databases in SQlite for Django.

I am following the django tutorial on ubuntu.

Now its working fine except. After running

python manage.py sql polls

then

python manage.py syncdb

So then I thought I would check out the daabase and tables but this is where the issue is:

sqlite> .databases
seq  name             file                                                      
---  ---------------  ----------------------------------------------------------
0    main                                                                       
1    temp    

There is no mysite database. How can i see the database?

回答1:

your mysite database will be in the file system itself in the root of your project (top most folder of your django project), if this is how your settings.py looks like:-

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mysite',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

If you enabled your django admin, and write the appropriate admin.py files for your polls app, you should be able to add, edit, delete or view your polls data in django admin.

You can of course load up your mysite database in your django project root (top folder) and view the data in it using something like http://sqlitebrowser.sourceforge.net/

In command line in your ubuntu terminal, if you did do your syncdb correctly, you should be seeing something similar to this:-

calvin$ ./manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'calvin'): calvin
E-mail address: myemail@myemail.com
Password: 
Password (again): 
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

You mentioned you already run your ./manage.py syncdb correctly, so you should be able access your sqlite database by executing sqlite mysite, like this:-

calvin$ sqlite3 mysite 
SQLite version 3.7.14.1 2012-10-04 19:37:12
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 

And the .tables command in your sqlite shell will give you:-

sqlite> .tables
auth_group                  auth_user_user_permissions
auth_group_permissions      django_content_type       
auth_permission             django_session            
auth_user                   django_site               
auth_user_groups          
sqlite> 


回答2:

I have used option 'dbshell' if this is already synced:

python manage.py dbshell

SQLite version 3.8.2 2013-12-06 14:53:30 Enter ".help" for instructions Enter SQL statements terminated with a ";"

sqlite> .databases



回答3:

simply go to your project folder there is ds.sqlite3 file

open this in tool from https://sqlitebrowser.org/

Inside tool > Browse Data > select table in drop down to view table