數(shù)據(jù)庫登錄命令
mysql -u?root -p
show databases;
Django連接數(shù)據(jù)庫
在settings.py文件中進(jìn)行配置和修改
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1', # 數(shù)據(jù)庫主機(jī)
'PORT': 3306, # 數(shù)據(jù)庫端口
'USER': 'root', # 數(shù)據(jù)庫用戶名
'PASSWORD': '123456', # 數(shù)據(jù)庫用戶密碼
'NAME': 'gx_day15' # 數(shù)據(jù)庫名字
}
}
Django創(chuàng)建表
在models.py文件中進(jìn)行修改
創(chuàng)建表
執(zhí)行命令:
python manage.py makemigrations
python manage.py migrate
條件:在setings保證app已經(jīng)注冊
刪除表
當(dāng)需要刪除表時(shí),則再models.py中注釋表結(jié)構(gòu),然后重新執(zhí)行命令即可。
修改表
新增列的時(shí)候,由于已存在列中可能已有數(shù)據(jù),所以新增列必須指定新增列的對應(yīng)數(shù)據(jù)
age=models.InterField(default=2)?默認(rèn)為2
age=models.InterField(null = True ,blank =True)?默認(rèn)為空
操作表中的數(shù)據(jù)
desc?表名
文章來源:http://www.zghlxwxcb.cn/news/detail-859791.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-859791.html
到了這里,關(guān)于Django連接數(shù)據(jù)庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!