django 連接數(shù)據(jù)庫(kù) sqlite的例子
Aphorism
the fight is worth it.
django models 連接 sqlite 數(shù)據(jù)庫(kù)
django 版本為 1.11.7
在 blog 項(xiàng)目下創(chuàng)建一個(gè) app article :python manage.py startapp article
在 blog 項(xiàng)目結(jié)構(gòu)下會(huì)多出一個(gè) article 目錄
在 article 下面的 models.py 文件中輸入
from django.db import models
class Article(models.Model):
name = models.CharField('名稱',max_length = 30)
age = models.CharFiels('年齡',max_length = 5)
class Meta:
db_table = 'Article'
step4: 在 子blog 目錄中 修改 setting.py 文件
- 要連接到哪種數(shù)據(jù)庫(kù) : sqlite ? mysql? - 連接的數(shù)據(jù)庫(kù)路徑
下面這種配置表示:
1. sqlite 數(shù)據(jù)庫(kù) 和 在項(xiàng)目根目錄下 創(chuàng)建一個(gè) article.db 數(shù)據(jù)庫(kù)文件
2. 數(shù)據(jù)庫(kù)的類型 以及 數(shù)據(jù)庫(kù)的存儲(chǔ)位置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'article.db'),
}
}
step5: 執(zhí)行 python manage.py makemigrations
改動(dòng)了 model.py的內(nèi)容之后執(zhí)行下面的命令:python manger.py makemigrations相當(dāng)于 在該app下建立 migrations目錄,并記錄下你所有的關(guān)于modes.py的改動(dòng),比如0001_initial.py, 但是這個(gè)改動(dòng)還沒(méi)有作用到數(shù)據(jù)庫(kù)文件
cmd 中會(huì)顯示
Migrations for 'article': article\migrations\0001_initial.py - Create model Article
step6: 執(zhí)行 python manage.py migrate
在第5步之后執(zhí)行命令 將該models.py改動(dòng) 作用到數(shù)據(jù)庫(kù)文件,說(shuō)明 /blog/article數(shù)據(jù)庫(kù)文件已經(jīng)被改變了,生成表格或者操作了數(shù)據(jù)
cmd 中 顯示:
(blog) C:\Users\by\Desktop\review\python\blog\blog>python manage.py migrate Operations to perform: Apply all migrations: admin, article, articles, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying article.0001_initial... OK Applying articles.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying sessions.0001_initial... OK
step7: 執(zhí)行 python manage.py sqlmigrate app_name 0001
這個(gè)命令用于查看 生成的 sql語(yǔ)句, 這個(gè)命令相當(dāng)于之前 django 版本中的 sqlall
cmd 中顯示:
(blog) C:\Users\by\Desktop\review\python\blog\blog>python manage.py sqlmigrate article 0001
BEGIN;
--
-- Create model Article
--
CREATE TABLE "Article" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(30) NOT NULL, "age" varchar(5) NOT NULL);
COMMIT;
step8: 執(zhí)行命令 python manage.py dbshell
可以看看數(shù)據(jù)中是否生成了對(duì)應(yīng)的表,顯然本例子中 Article 生成,Articles 是我之前啟動(dòng)的另一個(gè) app
或者 sqlite3 article.db 然后 .tables也可以查看
cmd 中
(blog) C:\Users\by\Desktop\review\python\blog\blog>python manage.py dbshell SQLite version 3.7.15.2 2013-01-09 11:53:05 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .tables Article auth_user_groups Articles auth_user_user_permissions auth_group django_admin_log auth_group_permissions django_content_type auth_permission django_migrations auth_user
使用 python shell insert 數(shù)據(jù)到 article.db 中
execute python manage.py shell 進(jìn)入項(xiàng)目的 python 命令執(zhí)行環(huán)境中
使用python 語(yǔ)法往數(shù)據(jù)庫(kù) insert 數(shù)據(jù)
>>> from article.models import Article ## 等價(jià)寫法: from articles import models.Article >>> Article.objects.create(name='tom',age='12') <Article: Article object> >>> Article.objects.create(name='juice',age='13') <Article: Article object>
查看article.db 數(shù)據(jù)庫(kù):
sqlite article.db select * from article sqlite> select * from Article ...> ; 1|tom|12 2|juice|13 sqlite>
以上這篇django 連接數(shù)據(jù)庫(kù) sqlite的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python自動(dòng)化辦公之圖片轉(zhuǎn)PDF的實(shí)現(xiàn)
實(shí)現(xiàn)圖片轉(zhuǎn)換成PDF文檔的操作方法有很多,綜合對(duì)比以后感覺(jué)fpdf這個(gè)模塊用起來(lái)比較方便而且代碼量相當(dāng)少。所以本文將利用Python語(yǔ)言實(shí)現(xiàn)圖片轉(zhuǎn)PDF,感興趣的可以了解一下2022-04-04
python通過(guò)移動(dòng)端訪問(wèn)查看電腦界面
這篇文章主要介紹了python通過(guò)移動(dòng)端訪問(wèn)查看電腦界面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
使用Django和Python創(chuàng)建Json response的方法
下面小編就為大家分享一篇使用Django和Python創(chuàng)建Json response的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
python使用openpyxl庫(kù)修改excel表格數(shù)據(jù)方法
今天小編就為大家分享一篇python使用openpyxl庫(kù)修改excel表格數(shù)據(jù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python計(jì)算階乘和的方法(1!+2!+3!+...+n!)
今天小編就為大家分享一篇python計(jì)算階乘和的方法(1!+2!+3!+...+n!),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果示例
這篇文章主要介紹了Python實(shí)現(xiàn)PS圖像調(diào)整黑白效果,結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)PS圖像的黑白效果原理與相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

