Django+python3.3 如何连接至mysql或者postgres? 求教详细方法

2025-06-24 11:33:25
推荐回答(1个)
回答1:

#
#settings.py 里配置:
#
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'dbname',                      # Or path to database file if using sqlite3.
        'USER': 'uname',                      # Not used with sqlite3.
        'PASSWORD': 'pwd',                  # Not used with sqlite3.
        'HOST': '127.0.0.1',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '3306',                      # Set to empty string for default. Not used with sqlite3.
    }
}

#
#用MySQLdb
#
conn=MySQLdb.connect(user='uname', db='dbname', passwd='pwd', host='127.0.0.1', use_unicode=True, charset='utf8')