pip install django
pip install psycopg2
pip install mysqlclient
Entity.py
from django.db import models
from django.conf import settings
import django
INSTALLED_APPS = [
"Entity"
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'baby_name',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
settings.configure(DATABASES=DATABASES, INSTALLED_APPS=INSTALLED_APPS)
django.setup()
"""
Name Model
"""
class Name(models.Model):
# name = models.CharField(db_column=u'id', max_length=512, primary_key=true)
name = models.TextField(db_column=u'name') #name 是字段名称
# name = models.CharField(max_length=200)
# id = models.CharField(max_length=200)
# pub_date = models.DateTimeField('date published')
def __unicode__(self):
return 'name'
## table_name
class Meta:
db_table = 'name'
test_db.py
from Entity import Name
def getNames():
all_baby_names = Name.objects.all()
for i in all_baby_names:
print(i.name)
if __name__ == "__main__":
getNames()
两个文件放在同一个目录...