django 连接数据库 sqlite的例子


Posted in Python onAugust 14, 2019

Aphorism

the fight is worth it.

django models 连接 sqlite 数据库

django 版本为 1.11.7

在 blog 项目下创建一个 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 文件

- 要连接到哪种数据库 : sqlite ? mysql?
- 连接的数据库路径

下面这种配置表示:

1. sqlite 数据库 和 在项目根目录下 创建一个 article.db 数据库文件

2. 数据库的类型 以及 数据库的存储位置

DATABASES = {
  'default': {
   'ENGINE': 'django.db.backends.sqlite3',
   'NAME': os.path.join(BASE_DIR, 'article.db'),
  }
 }

step5: 执行 python manage.py makemigrations

改动了 model.py的内容之后执行下面的命令:python manger.py makemigrations相当于 在该app下建立 migrations目录,并记录下你所有的关于modes.py的改动,比如0001_initial.py, 但是这个改动还没有作用到数据库文件

cmd 中会显示

Migrations for 'article':
 article\migrations\0001_initial.py
 - Create model Article

step6: 执行 python manage.py migrate

在第5步之后执行命令 将该models.py改动 作用到数据库文件,说明 /blog/article数据库文件已经被改变了,生成表格或者操作了数据

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: 执行 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: 执行命令 python manage.py dbshell

可以看看数据中是否生成了对应的表,显然本例子中 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 数据到 article.db 中

execute python manage.py shell 进入项目的 python 命令执行环境中

使用python 语法往数据库 insert 数据

>>> 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 数据库:

sqlite article.db
select * from article
sqlite> select * from Article
 ...> ;
1|tom|12
2|juice|13
sqlite>

以上这篇django 连接数据库 sqlite的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python基础教程之lambda表达式使用方法
Feb 12 Python
linux下python抓屏实现方法
May 22 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
Oct 25 Python
Python实现单词翻译功能
Jun 06 Python
python 实现tar文件压缩解压的实例详解
Aug 20 Python
在python中利用numpy求解多项式以及多项式拟合的方法
Jul 03 Python
python中的RSA加密与解密实例解析
Nov 18 Python
jupyter note 实现将数据保存为word
Apr 14 Python
Matplotlib 折线图plot()所有用法详解
Jul 28 Python
Python使用socket模块实现简单tcp通信
Aug 18 Python
python 通过使用Yolact训练数据集
Apr 06 Python
python中redis包操作数据库的教程
Apr 19 Python
Python将主机名转换为IP地址的方法
Aug 14 #Python
Python利用WMI实现ping命令的例子
Aug 14 #Python
使用Windows批处理和WMI设置Python的环境变量方法
Aug 14 #Python
python for循环remove同一个list过程解析
Aug 14 #Python
如何用Python来理一理红楼梦里的那些关系
Aug 14 #Python
django之自定义软删除Model的方法
Aug 14 #Python
python实现登录密码重置简易操作代码
Aug 14 #Python
You might like
PHP脚本的10个技巧(2)
2006/10/09 PHP
PHP执行速率优化技巧小结
2008/03/15 PHP
php中关于普通表单多文件上传的处理方法
2011/03/25 PHP
php多次include后导致全局变量global失效的解决方法
2015/02/28 PHP
PHP类的封装与继承详解
2015/09/29 PHP
PHP中大括号'{}'用法实例总结
2017/02/08 PHP
JS TextArea字符串长度限制代码集合
2012/10/31 Javascript
jQuery操作CheckBox的方法介绍(选中,取消,取值)
2014/02/04 Javascript
js通过八个点 拖动改变div大小的实现方法
2014/03/05 Javascript
jQuery判断当前点击的是第几个li的代码
2014/09/26 Javascript
AngularJS单选框及多选框实现双向动态绑定
2016/01/13 Javascript
jQuery 检查某个元素在页面上是否存在实例代码
2016/10/27 Javascript
javascript九宫格图片随机打乱位置的实现方法
2017/03/15 Javascript
微信小程序scroll-view仿拼多多横向滑动滚动条
2020/04/21 Javascript
微信小程序scroll-view横向滑动嵌套for循环的示例代码
2018/09/20 Javascript
详解关于Angular4 ng-zorro使用过程中遇到的问题
2018/12/05 Javascript
Node.js在图片模板上生成二维码图片并附带底部文字说明实现详解
2019/08/07 Javascript
Js图片点击切换轮播实现代码
2020/07/27 Javascript
Python使用Scrapy爬取妹子图
2015/05/28 Python
使用Python3编写抓取网页和只抓网页图片的脚本
2015/08/20 Python
Python实现字典排序、按照list中字典的某个key排序的方法示例
2018/12/18 Python
python中用logging实现日志滚动和过期日志删除功能
2019/08/20 Python
Python面向对象编程基础实例分析
2020/01/17 Python
python 读取二进制 显示图片案例
2020/04/24 Python
基于FME使用Python过程图解
2020/05/13 Python
scrapy框架携带cookie访问淘宝购物车功能的实现代码
2020/07/07 Python
python matplotlib绘制三维图的示例
2020/09/24 Python
使用AJAX和Django获取数据的方法实例
2020/10/25 Python
Timberland澳大利亚官网:全球领先的户外品牌
2019/12/10 全球购物
PHP如何去执行一个SQL语句
2016/03/05 面试题
酒店仓管员岗位职责
2014/04/28 职场文书
学用政策心得体会
2014/09/10 职场文书
专职安全员岗位职责
2015/04/11 职场文书
电子表的操作介绍说明书
2019/10/28 职场文书
PHP连接MSSQL数据库案例,PHPWAMP多个PHP版本连接SQL Server数据库
2021/04/16 PHP
MySQL 常见存储引擎的优劣
2021/06/02 MySQL