SQLite 3 Database with Django

不问归期 提交于 2020-12-03 07:35:41

问题


I am stuck on Django Documentation tutorial on displaying SQLite table.

" If you’re interested, run the command-line client for your database and type \dt (PostgreSQL), SHOW TABLES; (MySQL), or .schema (SQLite) to display the tables Django created. "

I have created a project named mysite. Location : C:\Python34\Scripts\mysite

Inside mysite, there are mysite folder, db.sqlite3, and manage.py.

I opened command prompt and navigate to C:\Python34\Scripts\mysite and I type .schema and it returns " . schema is not recognized.. "

My settings.py database file :

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

I also can't define BASE_DIR. I don't know about the db.sqlite3 file. I cannot know if the file is .db extension.

Could someone help me to find out ?


回答1:


.schema is a command you would run inside of the sqlite3 command line interface to get a list of tables in that database.

If you don't already have it installed, you can download sqlite3 command line interface here and install it: https://www.sqlite.org/download.html

Once you have sqlite3 installed, you can:

  • Change directories to the folder containing your sqlite3 database file

    cd C:\Python34\Scripts\mysite

  • Open the database file with the sqlite3 command line interface

    sqlite3 db.sqlite3

  • Get a list of tables by typing .schema at the sqlite3 command line interface prompt

    .schema




回答2:


You didn't "run the command-line client for your database", you simply navigated to the directory. The command-line client is "sqlite3", although you may not have it installed on Windows.

You don't need to define BASE_DIR, it is already defined higher up in the settings file, but you do need to refer to it as a variable, not a string:

'NAME': os.path.join(BASE_DIR , 'db.sqlite3'),



回答3:


BASE_DIR is not a string. It is a variable whose value is automatically declared by django in settings.py file. You just need to remove the quotes in your usage of BASE_DIR.

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



回答4:


BASE_DIR is variable and not string, add import os and BASE_DIR case you need

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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



回答5:


You may have to change in settings.py file for Database. Place 'NAME':'db.sqllite3', in Database dict.

This will create db.sqlite3 file in your project dir. It has resolved my issue. Hope this helps.



来源:https://stackoverflow.com/questions/31762878/sqlite-3-database-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!