django 连接数据库 sqlite的例子

yipeiwu_com6年前Python基础

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 类对象和实例对象动态添加方法(分享)

实例如下所示: class Person(): def __init__(self, name): self.name = name def print_name(self)...

python 读写文件包含多种编码格式的解决方式

今天写一个脚本文件,需要将多个文件中的内容汇总到一个txt文件中,由于多个文件有三种不同的编码方式,读写出现错误,先将解决方法记录如下: # -*- coding: utf-8 -*...

python基础教程项目五之虚拟茶话会

python基础教程项目五之虚拟茶话会

几乎在学习、使用任何一种编程语言的时候,关于socket的练习从来都不会少,尤其是会写一些局域网的通信的东西。所以书上的这个项目刚好可以练习一下socket编程。 这个练习的整体思路首先...

Python 实现一行输入多个值的方法

Python 实现一行输入多个值的方法

python如何实现一行输入多个值呢? 例如 读入两个数10 23到a b  中。 很简单   a,b = input().split()  即可...

Python实现的一个找零钱的小程序代码分享

Python写的一个按面值找零钱的程序,按照我们正常的思维逻辑从大面值到小面值的找零方法,人民币面值有100元,50元,20元,10元,5元,1元,5角,1角,而程序也相应的设置了这些面...