settings.py 設置數(shù)據(jù)庫
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
'default': {
'ENGINE': 'django.db.backends.mysql', # 數(shù)據(jù)庫引擎
'NAME': 'study', # 數(shù)據(jù)庫名字
'USER': 'root', # 用戶名
'PASSWORD': 'mysqlgame123', # 密碼
'HOST': 'xxxxxx', # HOST
'PORT': '3306', # 端口
'OPTIONS': {'charset': 'utf8mb4'}, # 打開數(shù)據(jù)庫 編碼格式 ——解決4字節(jié)表情無法儲存問題
}
}
這樣后續(xù)的操作都會在這個數(shù)據(jù)庫中文章來源:http://www.zghlxwxcb.cn/news/detail-758972.html
自定義sql查詢
這里在一些多表關聯(lián)的時候是特別特別有用的,也是實際開發(fā)中不可避免的知識點文章來源地址http://www.zghlxwxcb.cn/news/detail-758972.html
- with 寫法
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM app_grade WHERE grade_name='二班'")
rows = cursor.fetchall()
for row in rows:
print(row)
- 普通寫法
cursor=connection.cursor()
cursor.execute("SELECT * FROM app_grade WHERE grade_name='二班'")
rows = cursor.fetchall()
for row in rows:
print(rows)
cursor.close()
到了這里,關于[Django-05 ]自定義sql查詢的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!