美文网首页
Django笔记12:SQLite数据类型

Django笔记12:SQLite数据类型

作者: _百草_ | 来源:发表于2021-12-24 21:02 被阅读0次

1. SQLite简介

  • what
    SQL数据库引擎
    进程内的,无需系统中配置(直接访问存储文件)、无服务器、事务性
  • why
  1. 无服务器,即不需单独的服务器进程或操作的系统
  2. 零配置,即不需要安装或管理
  3. 轻量级
  4. 自给自足,无需外部依赖
  5. 多平台,window、unix(linux、ios、android……)

事务:四大特性AIDC

  • 原子性(Atomicity):全部完成,或全部取消。若事务崩溃,状态回到事务之前,即事务回滚。
  • 隔离性(Isolation):事务T1和T2同时运行,无论T1和T2谁先结束,最终结果都是相同的。
  • 持久性(Durability):一旦事务提交,不管发生什么(崩溃或出错),数据要保存在数据库中。
  • 一致性(Consistency):只有合法的数据(依照关系约束和函数约束)才能写入数据库
  • SQLite命令
    DDL - 数据定义语言
CREATE  # 创建表、视图或其他数据库对象
ALTER  # 修改已有的数据库对象,如表
DROP # 删除数据库对象,如表

DML - 数据操作语言

INSERT  # 创建记录
UPDATE  # 修改记录
DELETE  # 删除记录

DQL - 数据查询语言

SELECT  # 查询记录

2. SQLite命令

Django笔记10:sqlite使用合集

2.7 .help 获取命令清单
sqlite> .help
.backup ?DB? FILE        Backup DB (default "main") to FILE  #备份数据库默认main到file
.bail on|off             Stop after hitting an error.  Default OFF  #发生错误时停止。默认OFF
.databases               List names and files of attached databases  #查看数据库的名称及其依附的文件
.dump ?TABLE? ...        Render all database content as SQL  # 以SQL文本格式转存数据库。若指定了TABLE表,则指转储匹配LIKE模式的TABLE表
.echo on|off             Turn command echo on or off  # 开启或关闭echo命令
.exit ?CODE?             Exit this program with return-code CODE  # 退出
.headers on|off          Turn display of headers on or off # 开启或关闭头部显示
.help ?-all? ?PATTERN?   Show help text for PATTERN  # 显示消息
.import FILE TABLE       Import data from FILE into TABLE # 导入来自FILE文件的数据到TABLE
.indexes ?TABLE?         Show names of indexes  # 显示所有索引的名称。若是指定了TABLE,则只显示匹配LIKE模块的TABLE表的索引。
.log FILE|off            Turn logging on or off.  FILE can be stderr/stdout  # 开启或关闭日志
.mode MODE ?TABLE?       Set output mode  # 设置输出模式
.nullvalue STRING        Use STRING in place of NULL values  # 在NULL值的地方输出STRING字符串
.output ?FILE?           Send output to FILE or stdout if FILE is omitted  # 输出到FILE文件或发送到屏幕(.output stdout)
.print STRING...         Print literal STRING  # 逐字输出到STRING字符串
.prompt MAIN CONTINUE    Replace the standard prompts  # 替换标准提示符
.quit                    Exit this program   # 退出SQLite提示符
.read FILE               Read input from FILE  # 执行FILE中的SQL
.schema ?PATTERN?        Show the CREATE statements matching PATTERN  # 显示CREATE语句。若指定了TABLE表,则只显示匹配LIKE模式的TABLE表。
.separator COL ?ROW?     Change the column and row separators # 改变输出模式和.import所使用的分隔符
.show                    Show the current values for various settings  # 显示各项设置的当前值
.stats ?on|off?          Show stats or turn stats on or off  # 开启或关闭统计
.tables ?TABLE?          List names of tables matching LIKE pattern TABLE  # 列出匹配LIKE模式的表的名称
.timeout MS              Try opening locked tables for MS milliseconds  # 尝试打开锁定的表MS毫秒
.timer on|off            Turn SQL timer on or off  # 开启或关闭定时器
.width NUM1 NUM2 ...     Set column widths for "column" mode  # 为column模式设置列宽度



.auth ON|OFF             Show authorizer callbacks
.binary on|off           Turn binary output on or off.  Default OFF
.cd DIRECTORY            Change the working directory to DIRECTORY
.changes on|off          Show number of rows changed by SQL
.check GLOB              Fail if output since .testcase does not match
.clone NEWDB             Clone data into NEWDB from the existing database
.dbconfig ?op? ?val?     List or change sqlite3_db_config() options
.dbinfo ?DB?             Show status information about the database
.eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN
.excel                   Display the output of next command in a spreadsheet
.expert                  EXPERIMENTAL. Suggest indexes for specified queries
.fullschema ?--indent?   Show schema and the content of sqlite_stat tables
.imposter INDEX TABLE    Create imposter table TABLE on index INDEX
.limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT
.lint OPTIONS            Report potential schema issues.
.once (-e|-x|FILE)       Output for the next SQL command only to FILE
.open ?OPTIONS? ?FILE?   Close existing database and reopen FILE
.parameter CMD ...       Manage SQL parameter bindings
.progress N              Invoke progress handler after every N opcodes
.restore ?DB? FILE       Restore content of DB (default "main") from FILE
.save FILE               Write in-memory database into FILE
.scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off
.selftest ?OPTIONS?      Run tests defined in the SELFTEST table
.sha3sum ...             Compute a SHA3 hash of database content
.shell CMD ARGS...       Run CMD ARGS... in a system shell
.system CMD ARGS...      Run CMD ARGS... in a system shell
.testcase NAME           Begin redirecting output to 'testcase-out.txt'
.trace ?OPTIONS?         Output each SQL statement as it is run
.vfsinfo ?AUX?           Information about the top-level VFS
.vfslist                 List all available VFSes
.vfsname ?AUX?           Print the name of the VFS stack
sqlite>
2.8 查看数据库

.databases : List names and files of attached databases # 查看数据库的名称及其依附的文件

sqlite> .databases
main: C:\testing\py\PycharmProjects\apiPlatform\db.sqlite3
sqlite>
2.9 执行文件中SQL

sql.txt 文件中sql

.header on
.mode column
select * from auth_user;

执行.read sql.txt
结果:

read FILE
2.10 查看SQLite模式设置
显示当前各个模式设置
2.11 显示CPU定时器
sqlite> .timer on
sqlite> select * from auth_user;
id          password                                                                                  last_login  is_superuser  username    last_name   ema
il       is_staff    is_active   date_joined                 first_name
----------  ----------------------------------------------------------------------------------------  ----------  ------------  ----------  ----------  ---
-------  ----------  ----------  --------------------------  ----------
1           pbkdf2_sha256$260000$XX              1             wlh                     123
@qq.com  1           1           2021-12-23 09:03:54.925752
Run Time: real 0.003 user 0.000000 sys 0.000000
sqlite>

3. SQLite语法

  • 不区分大小写
  • 注释
    --开始注释

参考:

  1. SQLite 简介
  2. SQLite 安装
  3. SQLite命令
  4. SQLite语法

相关文章

网友评论

      本文标题:Django笔记12:SQLite数据类型

      本文链接:https://www.haomeiwen.com/subject/blvviltx.html