Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sqlite use in terminal -django

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?

like image 695
sayth Avatar asked Nov 03 '12 12:11

sayth


People also ask

Can you use SQLite in Python?

Python SQLite3 module is used to integrate the SQLite database with Python. It is a standardized Python DBI API 2.0 and provides a straightforward and simple-to-use interface for interacting with SQLite databases. There is no need to install this module separately as it comes along with Python after the 2.5x version.

How do I use SQLite in terminal?

Start the sqlite3 program by typing "sqlite3" at the command prompt, optionally followed by the name the file that holds the SQLite database (or ZIP archive). If the named file does not exist, a new database file with the given name will be created automatically.


2 Answers

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: [email protected]
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> 
like image 111
Calvin Cheng Avatar answered Oct 19 '22 19:10

Calvin Cheng


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

like image 26
shashankS Avatar answered Oct 19 '22 20:10

shashankS