Analyzing the database

In most modern Linux distributions and macOS, SQLite is already installed, and therefore you can run the sqlite3 command-line utility.

In Windows, if you want to work with the sqlite3.exe command-line utility, you have to download the bundle of command-line tools for managing SQLite database files from the downloads section of the SQLite webpage at http://www.sqlite.org/download.html. For example, the ZIP file that includes the command-line tools for version 3.20.1 is sqlite-tools-win32-x8 6-3200100.zip. The name for the file changes with the SQLite version. You just need to make sure that you download the bundle of command-line tools and not the ZIP file that provides the SQLite DLLs. After you unzip the file, you can include the folder that includes the command-line tools in the PATH environment variable, or you can access the sqlite3.exe command-line utility by specifying the full path to it.

Run the following command to list the generated tables. The first argument, db.sqlite3, specifies the file that contains that SQLite database and the second argument indicates the command that we want the sqlite3 command-line utility to run against the specified database:

    sqlite3 db.sqlite3 ".tables" 

The following lines show the output for the previous command with the list of tables that Django generated in the SQLite database:

    auth_group                  django_admin_log
    auth_group_permissions      django_content_type
    auth_permission             django_migrations
    auth_user                   django_session
    auth_user_groups            toys_toy
    auth_user_user_permissions  

The following command will allow you to check the contents of the toys_toy table after we compose and send HTTP requests to the RESTful Web Service and the web service makes CRUD operations to the toys_toy table:

    sqlite3 db.sqlite3 "SELECT * FROM toys_toy ORDER BY name;" 

Instead of working with the SQLite command-line utility, you can use a GUI tool to check the contents of the SQLite database. DB Browser for SQLite is a useful, free, multiplatform GUI tool that allows us to easily check the database contents of an SQLite database in Linux, macOS, and Windows. You can read more information about this tool and download its different versions from http://sqlitebrowser.org. Once you have installed the tool, you just need to open the db.sqlite3 file and you can check the database structure and browse the data for the different tables. After we start working with the first version of our web service, you need to check the contents of the toys_toy table with this tool.

You can also use the database tools included with your favorite IDE to check the contents of the SQLite database.

The SQLite database engine and the database file name are specified in the restful01/settings.py Python file. The following lines show the declaration of the DATABASES dictionary, which contains the settings for all the databases that Django uses. The nested dictionary maps the database named default with the django.db.backends.sqlite3 database engine and the db.sqlite3 database file located in the BASE_DIR folder (restful01):

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

After we execute the migrations, the SQLite database will have the following tables. Django uses prefixes to identify the modules and applications that each table belongs to. The tables that start with the auth_ prefix belong to the Django authentication module. The table that starts with the toys_ prefix belongs to our toys application. If we add more models to our toys application, Django will create new tables with the toys_ prefix:

  • auth_group: Stores authentication groups
  • auth_group_permissions: Stores permissions for authentication groups
  • auth_permission: Stores permissions for authentication
  • auth_user: Stores authentication users
  • auth_user_groups: Stores authentication user groups
  • auth_user_groups_permissions: Stores permissions for authentication user groups
  • django_admin_log: Stores the Django administrator log
  • django_content_type: Stores Django content types
  • django_migrations: Stores the scripts generated by Django migrations and the date and time at which they were applied
  • django_session: Stores Django sessions
  • toys_toy: Persists the Toys model
  • sqlite_sequence: Stores sequences for SQLite primary keys with autoincrement fields
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.46.141