I've produced a Django application.Now i'm attempting to populate some data into SQL database tabel EmployeeDetails, using normal python script. But i'm not in a position to place data into db. I suppose its because my db isn't hooking up with this particular exterior python script. I'm new with Python, so help me to resolve this.This really is my python script :

import MySQLdb
    db=MySQLdb.connect("localhost","root","password123","employee")
    def dumpdata():
        userName = 'John'
        designation = 'Software Engineer'
        employeeID = '2312'
        contactNumber = '9495321257'
        project = 'cricket'
        dateOfJoin = '2009-10-10'
        EmployeeDetails(userName,designation,employeeID,contactNumber,project,dateOfJoin).save()

My configurations.py in Django is :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'employee',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'password123',                  # 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.
    }
}

My final require is to place the insertion code inside a loop and populate as numerous data i want into db. Somebody please consider my code and assist me to to resolve this.

  1. Don't import MySQLdb. Use Django rather.

  2. Think before hooking up towards the database directly. Use Django rather.

  3. If you are planning to produce objects out of your models, you have to import individuals. They'll take proper care of posting all the necessary Django modules, for those who have written them properly.

  4. Model.objects.create() is really a shorthand for developing a new object and saving it.

Something similar to this:

from myapp.models import EmployeeDetails

def dumpdata():
    userName = 'John'
    designation = 'Software Engineer'
    employeeID = '2312'
    contactNumber = '9495321257'
    project = 'cricket'
    dateOfJoin = '2009-10-10'
    EmployeeDetails.objects.create(userName,designation,employeeID,contactNumber,project,dateOfJoin)

Or this (using keyword arguments implies that it's not necessary to recall the exact order of parameters to produce the item):

EmployeeDetails.objects.create(
    userName='John',
    designation='Software Engineer',
    employeeID='2312',
    contactNumber='9495321257',
    project='cricket',
    dateOfJoin='2009-10-10')

You will need to run by using the Django package in your Python path, with your DJANGO_SETTINGS_MODULE atmosphere variable set properly. Something similar to this may even work for me personally:

PYTHONPATH=. DJANGO_SETTINGS_MODULE=settings python my_script.py

But that invocation depends a great deal in your system setup.

Perhaps you have installed MySQL support for python yet?

Just in case you are on Home windows, the easiest is always to make use of a precompiled distribution - this a person's restricted to Python 2.6. I've not found any precompiled version for greater Python versions.