Python系列视频教程: Django【13讲】第七讲 数据库使用1
1.安装mysql-python
easy_install mysql-python
校验安装是否成功:
import MySQLdb
注意:下面安装失败了
pip install mysql-python
直接使用下载下来的安装包也失败了
MySQL-python-1.2.4b4.win32-py2.7.exe
报错,说注册表里面没有找到python2.7
2.创建一个database
mysql -uroot
mysql>create database csvt default charset =utf8;
3.settings.py
https://docs.djangoproject.com/en/1.6/ref/settings/
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'my.cnf'),
},
}
}
# my.cnf
[client]
database = csvt
user = root
password =
default-character-set = utf8
DATABASES = {
'default':{
'ENGINE': 'django.db.backends.mysql',
'NAME': 'csvt',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
DATABASES = {
'default':
{ 'ENGINE':'django.db.backends.oracle',
'NAME': 'xe',
'USER': 'a_user',
'PASSWORD':'a_password',
'HOST': 'dbprod01ned.mycompany.com',
'PORT': '1540', }
}
4.table的实现
blog\models.py
from django.db import models
# Create your models here.
class Employee(models.Model):
name = models.CharField(max_length=20)
#body = models.TextField()
#timestamp = models.DateTimeField()
D:\test\mysite\csvt03>python manage.py syncdb
mysql -uroot
>use csvt;
>show tables;
>desc blog_employee;