django 連接數(shù)據(jù)庫 sqlite的例子
Aphorism
the fight is worth it.
django models 連接 sqlite 數(shù)據(jù)庫
django 版本為 1.11.7
在 blog 項目下創(chuàng)建一個 app article :python manage.py startapp article
在 blog 項目結構下會多出一個 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ù)庫 : sqlite ? mysql? - 連接的數(shù)據(jù)庫路徑
下面這種配置表示:
1. sqlite 數(shù)據(jù)庫 和 在項目根目錄下 創(chuàng)建一個 article.db 數(shù)據(jù)庫文件
2. 數(shù)據(jù)庫的類型 以及 數(shù)據(jù)庫的存儲位置
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'article.db'), } }
step5: 執(zhí)行 python manage.py makemigrations
改動了 model.py的內(nèi)容之后執(zhí)行下面的命令:python manger.py makemigrations相當于 在該app下建立 migrations目錄,并記錄下你所有的關于modes.py的改動,比如0001_initial.py, 但是這個改動還沒有作用到數(shù)據(jù)庫文件
cmd 中會顯示
Migrations for 'article': article\migrations\0001_initial.py - Create model Article
step6: 執(zhí)行 python manage.py migrate
在第5步之后執(zhí)行命令 將該models.py改動 作用到數(shù)據(jù)庫文件,說明 /blog/article數(shù)據(jù)庫文件已經(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
這個命令用于查看 生成的 sql語句, 這個命令相當于之前 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ù)中是否生成了對應的表,顯然本例子中 Article 生成,Articles 是我之前啟動的另一個 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 進入項目的 python 命令執(zhí)行環(huán)境中
使用python 語法往數(shù)據(jù)庫 insert 數(shù)據(jù)
>>> from article.models import Article ## 等價寫法: 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ù)庫:
sqlite article.db select * from article sqlite> select * from Article ...> ; 1|tom|12 2|juice|13 sqlite>
以上這篇django 連接數(shù)據(jù)庫 sqlite的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python機器學習之神經(jīng)網(wǎng)絡(二)
這篇文章主要為大家詳細介紹了python機器學習之神經(jīng)網(wǎng)絡第二篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12淺談插入排序算法在Python程序中的實現(xiàn)及簡單改進
這篇文章主要介紹了插入排序算法在Python程序中的實現(xiàn)及簡單改進,插入排序算法的最差時間復雜度為O(n^2),最優(yōu)時間復雜度為O(n),存在一定的優(yōu)化空間,需要的朋友可以參考下2016-05-05極簡Python庫CherryPy構建高性能Web應用實例探索
今天為大家介紹的是 CherryPy,它是一個極簡、穩(wěn)定且功能強大的Web框架,可以幫助開發(fā)者快速構建高性能的 Web 應用程序,使用 CherryPy,你可以輕松地創(chuàng)建RESTful API、靜態(tài)網(wǎng)站、異步任務和 WebSocket 等應用2024-01-01Python實現(xiàn)將Excel轉換成xml的方法示例
這篇文章主要介紹了Python實現(xiàn)將Excel轉換成xml的方法,涉及Python針對Excel文件的讀取、遍歷、節(jié)點設置與xml生成等相關操作技巧,需要的朋友可以參考下2018-08-08Python利用PyQt5制作一個獲取網(wǎng)絡實時NBA數(shù)據(jù)并播報的GUI程序
現(xiàn)在NBA聯(lián)賽也進行到半決賽了,我們怎么樣才能以更快的方法獲取NBA的數(shù)據(jù)呢?這里我們就自己來做一個數(shù)據(jù)播報的程序,需要的朋友可以參考下2021-06-06python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解
這篇文章主要介紹了python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04