Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Transferring the Database

As mentioned at the beginning of the chapter, we created Hyperlink and Favorite models, and we created objects of each. Rather than repeat all that work, we'll copy the database file, favoritesdb, that was created in Chapter 3.

The copy process involves these steps:

  1. Copy favoritesdb.
  2. Copy models.py.
  3. Edit settings.py to create the database favoritesdb.
  4. Synchronize the database.

To transfer the database:

  1. Copy the database, favoritesdb, from the chapter3 folder to the chapter4 folder. This is the big step, because the favoritesdb database contains the tables and data that we'll use in this chapter.
  2. Copy models.py from chapter3\favorites to chapter4\favorites. (If your operating system asks whether you want to replace the existing models.py, answer Yes.)

    The models.py file contains the definition of the models you want to use. Unless you copy the version of models.py created in Chapter 3 that defines the models, you'll lose the models in the database when you synchronize the database.

  3. Using a text-editing program, open chapter4\settings.py and find the database section (Listing 4.3).

    Listing 4.3. The settings.py file.

    DATABASE_ENGINE = ''           #
          'postgresql_psycopg2',
          'postgresql', 'mysql', 'sqlite3'
          or 'oracle'.
    DATABASE_NAME = ''            # Or path
          to database file if using   sqlite3.
    DATABASE_USER = ''            # Not used
          with sqlite3.
    DATABASE_PASSWORD = ''        # Not used
          with sqlite3.
    
  4. Enter 'sqlite3' for the DATABASE_ENGINE setting and favoritesdb for the DATABASE_NAME setting (Listing 4.4).

    Listing 4.4. The edited settings.py file.

    
    DATABASE_ENGINE = 'sqlite3'           #
          'postgresql_psycopg2',
          'postgresql', 'mysql', 'sqlite3'
          or 'oracle'.
    DATABASE_NAME = 'favoritesdb'           #
          Or path
          to database file if using
          sqlite3.
    DATABASE_USER = ''             # Not used
          with sqlite3.
    DATABASE_PASSWORD = ''         # Not used
          with sqlite3.
    
  5. Save the file.
  6. Open a command prompt, and in the chapter4 directory, enter this command to set up the database system:
    $ python manage.py syncdb
    You will not be prompted to create a superuser account, since that account already exists in the chapter3 favoritesdb database.

Excellent—the database is set up, having been transferred from the chapter3 project.

  • + Share This
  • 🔖 Save To Your Account