2012-11-03 12 views
8

Come posso vedere i miei database in SQlite per Django.python sqlite usa nel terminale -django

Sto seguendo lo django tutorial su ubuntu.

Ora funziona bene eccetto. Dopo aver eseguito

python manage.py sql polls 

poi

python manage.py syncdb 

Allora ho pensato di controllare la daabase e tabelle, ma questo è dove il problema è:

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

non esiste un database mysite. Come posso vedere il database?

risposta

12

il database mysite sarà nel file system stesso nella root del vostro progetto (in alto la maggior parte della cartella del vostro progetto Django), se questo è il modo in cui il settings.py assomiglia: -

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. 
    } 
} 

Se è stata attivata il tuo amministratore di django e scrivi i file admin.py appropriati per l'app dei sondaggi, dovresti essere in grado di aggiungere, modificare, eliminare o visualizzare i dati dei tuoi sondaggi nell'amministratore di django.

Naturalmente, è possibile caricare il backup del database mysite nella directory principale del progetto Django (cartella) e visualizzare i dati in esso usando qualcosa come http://sqlitebrowser.sourceforge.net/

In linea di comando nel terminale ubuntu, se avete fatto fare il vostro syncdb correttamente, si dovrebbe vedere qualcosa di simile a questo: -

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) 

Lei ha detto che già esegue il ./manage.py syncdb correttamente, così si dovrebbe essere in grado di accedere al database SQLite eseguendo sqlite mysite, in questo modo: -

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> 

E il comando .tables nella shell SQLite vi darà: -

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> 
+1

Grazie, è fantastico. – sayth

+1

benvenuto. felice questa soluzione ha aiutato. –

5

Ho utilizzato l'opzione 'dbshell' se questo è già sincronizzato: versione

python manage.py dbshell

SQLite 3.8.2 2013-12-06 14:53:30 Immettere ".help" per istruzioni Immettere istruzioni SQL terminate con ";"

sqlite> .databases