SQLite Database Introduction

news2024/11/13 3:48:31
Use SQLite installed on Linux (CentOS7) to introduce.
(You can also use python to install SQLite3 for learning.)
SQLite Preface
Introduction to SQLite Features
SQLite Data Type
SQLite Constraint
SQLite Syntax
SQLite Join
SQLite Database & Table
SQLite Backup & Restore
SQLite Functions
Reference

  SQLite, a lightweight self-contained database, is an ACID-compliant relational database management system contained in a relatively small C library.

  ACID( Atomicity, Consistency, Isolation, Durability ).

  RDBMS( Relational Database Management System ).

1. ACID transactions.
2. Zero configuration - no need to install and manage configuration.
3. Supports most SQL92 standards, such as: ATTACH DATABASE, BEGIN TRANSACTION, COMMENT, etc., and it also supports transaction processing functions.
4. A complete database stored in a single disk file.
Suitable storage format for application files.
5. Database files can be freely shared between machines with different byte orders.
6. Support database size up to 2TB.
7. The source code is small, about 130,000 lines of C code, 4.44M.
Simple and easy to use API.
Available as a separate header package, easy to compile and easy to add to large projects.
8. Written in ANSI-C with TCL/TK bindings .
9. Faster operations on data than most popular databases.
10. Supports multiple platforms and multiple development languages: UNIX (Linux, Mac OS-X, Android, IOS), Windows (Win32, WinCE, WinRT), C, PHP, Perl, Java, C#, Python, Ruby, C++.
11. Command line interface for managing databases from the command line.
12. Standalone: ​​no additional dependencies.
SQLite Architecture:

 

   SQLite Execution Process:

SQLite first creates or opens a database, then executes the corresponding SQL statement, and finally closes the database.
In the Prepare phase, the input SQL statement is compiled into VDBE bytecode through the parser, tokenizer and code generator.
In the execution phase, the virtual machine executes the operator, the execution process is a step-by-step process, each step is started by the sqlite3_step function, and the sqlite3VdbeExec function executes the operator compiled by the Prepare phase.
In the Final stage, first execute the sqlite3VdbeFinalize function to close the VDBE, and then execute the sqlite_finalize () function to end the execution of SQLite.

 

The operation of the database is mainly to operate the table.

Each column of the table has a certain data type, such as integer value, string, Boolean, etc.

Data Type

Define

Data Type

Define

NULL

Indicates that the value is a NULL value.

double

64-bit real number

INTEGER

Unsigned integer value.

char(n)

A string of length n, n cannot exceed 254.

REAL

Floating point value.

varchar(n)

A string of variable length and its maximum length is n, where n cannot exceed 4000.

TEXT

Text string, the encoding method used for storage is UTF-8, UTF-16BE, UTF-16LE.

graphic(n)

Same as char(n), but its unit is two bytes and n cannot exceed 127. Such as Chinese.

BLOB

Store Blob data, this type of data is exactly the same as the input data, 1 means true, 0 means false.

vargraphic(n)

A variable-length double-character string with a maximum length of n, where n cannot exceed 2000.

smallint

16-bit integer

date

Contains year, month, date.

integer

32-bit integer

time

Contains hours, minutes, seconds.

decimal(p,s)

The exact value p refers to the number of decimal digits in all, and s refers to the number of decimal places after the decimal point. Default: p = 5, s = 0.

timestamp

Contains year, month, day, hour, minute, second, thousandth of a second.

float

32-bit real number

Each column of the table has some restrictive attributes, such as the data of some columns cannot be repeated, and some restrict the data range, etc.

Constraints are used to further describe the data attributes of each column.

Name

Define

NOT NULL

NOT NULL is a non-null field, which needs to be declared in advance when defining the table.

UNIQUE

In addition to the main column, there are also some columns that cannot have duplicate values.

PRIMARY KEY

Generally, it is an integer or a string, as long as it is guaranteed to be unique. In SQLite, if the primary key is an integer type, the value of the column can grow automatically.

FOREIGN KEY

A foreign key can be used to establish a relationship between two tables or more tables.

CHECK

Certain values ​​must meet certain conditions before they are allowed to be stored, which is why this CHECK constraint is required.

DEFAULT

There are some special field columns, in each record, the value is basically the same.

Operator

Illustrate

AND

This operator allows multiple conditions to be present (or used) in the WHERE clause of an SQL statement.

BETWEEN

This operator is used to search for values ​​that lie within a range of a given minimum and maximum value.

EXISTS

This operator is used to search for the existence of rows in the specified table that meet certain criteria.

IN

This operator is used to compare a value to a value in a specified list of literal values.

NOT IN

This operator is used to compare the negation of a value with a value in a specified list of literal values.

LIKE

This operator is used to compare a value with similar values ​​using the wildcard operator.

GLOB

This operator is used to compare values ​​with similar values ​​using wildcard operators.  ( GLOB != glob )

NOT

This operator reverses the meaning of the logical operator using it.

OR

This operator is used to combine multiple conditions in where clause of SQLite statement.

IS NULL

This operator is used to compare a value with an empty (null) value.

IS

This operator works like = operator.

IS NOT

This operator works like != operator.

||

This operator is used to concatenate two different strings on both sides of the operator to create a new string.

UNIQUE

This operator searches for the uniqueness of each row of records in the specified table (values ​​are not repeated).

SQLite is not case sensitive. -- However, there are some case-sensitive commands.

Comment:

Comments are used to increase code readability in SQLite code.
Comments cannot be nested.
Comments start with two consecutive "-" characters.
Sqlite > .table – This is a simple comment.
You can also start with a "/*" character and extend to the next "*/" character to treat the included content as a comment.

SQLite statement:

All SQLite statements start with keywords (such as: SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, etc.). All statements end with a semicolon (;).

Common SQLite Statements:

Case1: Create table:

CREATE TABLE table_name(
    column1 datatype,
    column2 datatype,
    column3 datatype,
    .....
    columnN datatype,
    PRIMARY KEY( one or more columns )); 

Case2: Select:

SELECT column1, column2....column
FROM table_name; 

Case3: Delete:

DELETE FROM table_name WHERE { CONDITION }; 

Case4: Update:

UPDATE table_name
SET column1 = value1, column2 = value2....columnN=valueN
[ WHERE CONDITION ]; 

Case5: Insert:

INSERT INTO 
table_name( column1, column2....column )
VALUES ( value1, value2....valueN ); 

Case6: Like:

SELECT column1, column2....column
FROM table_name
WHERE column_name LIKE { PATTERN }; 

Case7: Group:

SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name; 

Such as analyze, and/or, alter, between…

Operations overview:

Insert:

 Update:

Delete:

 Alert:

 Select:

In SQLite, the JOIN clause is used to combine records from two or more tables in a database.

It combines fields from both tables by using common values ​​from both tables.

There are mainly three types of joins in SQLite:

1. SQLite Inner Join
2. SQLite Outer Join
3. Sqlite Cross Join

Example:

If there are two tables

MEMBER:

 SALARY:

Inner Join:

It is used to combine all row records from multiple tables that satisfy the join condition.

SQLite inner join is the default join type.
Syntax
SELECT ... FROM table1 [INNER] JOIN table2 ON conditional_expression ...
Or
SELECT ... FROM table1 JOIN table2 USING ( column1 ,... ) ... 
Or
SELECT ... FROM table1 NATURAL JOIN table2... 

The shaded part is the intersection of inner joins:

Left Join:

  Similar to the INNER JOIN clause, the LEFT JOIN clause is an optional clause of the SELECT statement. Data from multiple related tables can be queried using the LEFT JOIN clause.

Syntax
SELECT ... FROM table1 LEFT JOIN table2 ON conditional_expression WHERE search_condition 

Outer Join:

In the SQL standard, there are three types of outer joins:

1. Left outer join
2. Right outer join
3. Full outer join

However, SQLite only supports left outer joins.

SQLite left outer join is used to fetch all rows from the left table specified in the ON condition and only those rows from the right table that satisfy the join condition are recorded.

Syntax
SELECT ... FROM table1 LEFT OUTER JOIN table2 ON conditional_expression
Or
SELECT ... FROM table1 LEFT OUTER JOIN table2 USING ( column1 ,... ) ... 

The shaded part is the intersection of left outer joins:

Cross Join:

SQLite cross join is used to match every row of the first table with every row of the second table. If the first table contains x columns and the second table contains y columns, the resulting cross join table results will contain x * y columns.

Syntax
SELECT ... FROM table1 CROSS JOIN table2

 Schematic:

Create Database:

Create a test database file in current directory.

 To Create it in a different directory, simply use the full path.

Attach a Database:

We can use the ATTACH DATABASE cmd to create a database from within the SQLite3 cmd utility.

Manipulate an existing database:

Create Table:

CREATE a TABLE

INSERT some items

Check the data

Rename table

 

Step:

.dump ->  Displays on the screen all the SQL statements for creating the table and inserting records into the table.
.output GARMINSE.sql ->  Specifies the filename of the dump command output to the file.
.dump à Output the SQL statement that creates and inserts data into the base table to the file specified by output.
.output stdout ->  Restore output to standard output device (screen).
.dump ->  At this point, the output SQL statement returns to the screen.
Drop table SE; ->  Delete SE table statement description.
.read GARMINSE.sql ->  Execute all the SQL statements contained in GARMINSE.sql to rebuild the table and related data that was just deleted.

  SQLite aggregate functions are functions that combine the values ​​of multiple rows as input for some condition and form a single value as the output result.

Number

Functions

Description

1

MIN()

The MIN() function is used to query the lowest (minimum) value of a column.

2

MAX()

The MAX() function is used to query the highest (maximum) value of a column.

3

AVG()

The AVG() function is used to query the average value of a column.

4

COUNT()

The COUNT() function is used to count the number of rows in a database table.

5

SUM()

The SQLite SUM() function is used to query the total number (sum of additions) of a specified numeric column.

6

RANDOM()

The RANDOM() function returns a pseudorandom integer between -9223372036854775808 and +9223372036854775807.

7

ABS()

The ABS() function is used to get the absolute value of a given parameter.

8

UPPER()

The UPPER() function is used to convert the given string argument to uppercase letters.

9

LOWER()

The LOWER() function is used to convert the given string argument to lowercase letters.

10

LENGTH()

The LENGTH() function is used to get the length of the given string.

Case1: SUM

Case2: COUNT

  Note: The content of SQLite is far more than that. Interested friends can also refer to the website link I posted, which has more detailed content.

Reference:

SQLite Home Page
https://zh.m.wikipedia.org/zh-hans/SQLite
bind manual page - Tk Built-In Commands
SQLite 注入 | 菜鸟教程
https://docs.python.org/zh-tw/3/library/sqlite3.html#
SQLite 教程 | 菜鸟教程
https://www.sqlitetutorial.net/

Reference

 Reference

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/141720.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第01讲:ElasticSearch安装

一、什么是ElasticSearch 二、ElasticSearch的安装 Elasticsearch 分为 Linux 和 Windows 版本,基于我们主要学习的是 Elasticsearch 的 Java 客户端的使用,所以课程中使用的是安装较为简便的 Windows 版本。 2.1、Windows系统下安装ElasticSearch W…

KubeSphere 使用 OpenLDAP 进行统一认证完全指南

作者:申红磊,青云QingCloud 容器解决方案架构师,开源项目爱好者,KubeSphere Member。 背景 在实际使用中,会有一些用户,在不同场景中经常碰到 OpenLDAP 对接问题: 能否对接 LDAP?对…

go sync.Map源码分析

概述 go 语言中的map并不是并发安全的,在Go 1.6之前,并发读写map会导致读取到脏数据,在1.6之后则程序直接panic. 因此之前的解决方案一般都是通过引入RWMutex(读写锁)进行处理, 关于go为什么不支持map的原子操作,概况来说,对map原子操作一定程度上降低了只有并发读,或不存在并…

多线程看这一篇文章就够了

第一章 多线程概述 1 2 31. 什么是程序? 2. 什么是进程? 3. 什么是线程?程序1是为完成特定任务、用某种语言编写的一组指令的集合(一段静态的代码)进程1是程序的一次执行过程,或是正在运行的一个程序线程1 2 3进程可进一步细化为线程,是一个程序内部的…

软考网络工程师上午常考点

软考网络工程师上午常考点: **计算机硬件基础:**根据考纲分析,本章主要考查三个模块:计算机体系结构、存储系统、I/O输入输出系统,其中每一模块又分若干知识点。“计算机硬件基础”相当于软考中的“公共基础课”&…

12月更新 | Visual Studio Code Python

我们很高兴地宣布,2022年12月发布的适用于 Visual Studio Code Python 和 Jupyter 扩展现已推出!此版本包括以下改进:Pylance 自动缩进 预览:浏览器中运行与调试 Python社区提供新扩展:Ruff如果您有兴趣,可…

C51单片机基础之串口编程实战

目录 一、串口编程寄存器分析 1、PCON : 电源控制寄存器 2、SCON:串行控制寄存器 二、自己实现串口初始化编程 三、发送一串字符串给到PC端编程 四、PC发送指令控制LED编程 五、串口中断实时控制LED编程 1、串口中断的中断号:interrupt4 2、串口…

Apache Doris Join 实现与调优实践|未来源码

推荐语: SQL 的支持力度和粒度,已经作为今天所有大数据计算引擎的重要衡量标准之一,而 SQL 的所有操作,可以分为简单操作(如 where、limit等 filter 操作)和复杂操作(groupby、join 等聚合操作&…

SpringCloud微服务项目实战 - 3.App端文章

经历了新冠疫情始末,之后,多出门走走,看看山,看看海,吃好吃的 系列文章目录 项目搭建App登录及网关App文章 文章目录系列文章目录一、文章列表1. 数据库⑴. 导入文章数据库⑵. 表结构分析①. ap_article 文章基本信息…

MySQL慢SQL探究

文章目录前言1、慢SQL捕获慢查询追踪配置方式2、情况分析为什么查询会慢?2.1 SQL执行计划分析explain执行计划分析PROFILE分析OPTIMIZER_TRACE分析3、引擎参数配置分析I/O性能分析MySQL I/O参数其他原因分析网络抖动单表数据量过大总结前言 我们在日常开发中&#…

GeoServer搭建私有地图服务,Cesium引擎加载。

一、安装JDK 1、安装GeoServer是基于Java的环境,所以需要先装Jdk环境。 2、前往官网下载Java SE 3、下载GeoServer 1、前往官网下载最新稳定版 2、安装GeoServer 二、发布地图服务 1、启动geoserver 找到安装目录,bin/下的startup.bat,双击执行…

PF-Net源码详解

代码及数据见最后 1.参数配置 参数配置使用默认参数即可,但是windows操作系统下,需要将--workers设置为0. 2.数据准备 PF-Net将不完整的点云做输入,并预测其缺失部分。同时,我们可以从整体流程看到,输入有三个尺度,通过最远点采样进行构建,预测的输出也有三个尺度。数…

MySQL:SQL通用语法、SQL分类、DDL、DML、DQL、DCL

一.SQL通用语法 SQL语句可以单行或多行书写,以分号结尾;SQL语句可以使用空格/缩进来增强语句的可读性;MySQL数据库SQL语句不区分大小写,关键字建议大写;注释: 单行:--或#多行:/* …

Spring AOP 面向切面编程

1.AOP是什么我们之前学过 OOP 面向对象编程, 那么 AOP 是否就比 OOP 更牛逼呢? 是否也是面向过程到面向对象这种质的跨越呢? 其实不是, 它和 OOP 思想是一种互补的关系, 是对 OOP 思想的一种补充.AOP (Aspect Oriented Programming) : 面向切面编程, 它是一种思想, 它是对某一…

Arrays数组

1.Arrays.toString()方法:输出数组内容 2.Arrays.sort()方法:给数组排序,默认升序 对其他对象数组进行排序 一个对象数组,排序算法需要重复比较数组中的元素。不同的类比较元素的规则是不同的,但是排序算法只应该调用类提供的比较方法&#…

netty中channelHandler实现原理及最佳实践|极客星球

为持续夯实MobTech袤博科技的数智技术创新能力和技术布道能力,本期极客星球邀请了企业服务研发部工程师梁立从 TCP 的粘包/半包、 Netty 处理粘包/半包及源码分析、 开源项目对 channelHandler最佳实践三方面对《netty 中channelHandler的原理与最佳实践》进行了全面…

【Ctfer训练计划】——(九)

作者名:Demo不是emo 主页面链接:主页传送门创作初心:舞台再大,你不上台,永远是观众,没人会关心你努不努力,摔的痛不痛,他们只会看你最后站在什么位置,然后羡慕或鄙夷座右…

Python+Selenium4元素定位_web自动化(3)

目录 0. 上节回顾 1. 八大定位 2. 定位器 3. CSS选择器 4. XPath选择器 4.1. XPath语法 4.2. XPath 函数 5. 相对定位 5.1 XPath 中的相对定位【重点】 5.1.1 相对路径 5.1.2 轴 5.2 selenium4 中的相对定位 总结 0. 上节回顾 浏览器的一般操作 浏览器的高级操作…

【sciter】:JSX 组件实现数据持久化

# 原理 组件数据持久化指的是:重新加载组件后,能否将重新加载前组件所存在的数据,在重新加载后数据依旧保存在组件中。 组件数据持久化实现原理:将每次更新组件数据同步到 Storage 中。并且监听组件重新加载(刷新),在刷新前将 Storage 关闭(确保数据不丢失)。当加载…

idea中添加git使用时文件不同颜色,标签不同颜色,代码不同颜色代表的含义

文章目录文件的颜色标签的颜色合并代码时不同颜色区块的含义文件的颜色 绿色——已经加入控制暂未提交; 红色——未加入版本控制;自己建立新文件后就是红色的,出现红色的一定要Add到git中,不然不能上传到远程仓库 蓝色——加入&am…