SQLite3介绍及SQL语句详解(SQLite一)

一、SQLite3介绍

1.数据库存储数据

数据结构 数据仓库:
在这里插入图片描述

2.数据库数据类型

名称数据长度类型
int-2^31(-2,147,483,648) 到 2^31(2,147,483,647)整型数字
float-1.79E+308到1.79E+308可变精度的数字
real-3.04E+38到3.04E+38可变精度的数字
char最大长度为8000定长非Unicode的字符型数据
varchar最大长度为8000变长非Unicode的字符型数据
text最大长度为2^31-1(2G)变长非Unicode的字符型数据

3.window下qtcreator上编译sqlite3数据

在qt creator上创建c语言工程,并且在工程中添加数据代码

把数据代码拷贝到工程目录下在添加–把工程中的main.c里面的主函数名修改下main_test 避免冲突
在这里插入图片描述
如果是linux系统要在工程文件中添加 LIBS += -lpthread -ldl

二、SQL语句

数据通用语句 命令行语句结束要加;(分号)

以此为例:

varchar(256)varchar(256)textchar(32)
number(编号)name(姓名)address(地址)QQ
20200101张三广州911683830
20200102李四广州911683831

1.创建数据表格

create  table  表名( 字段名 数据类型  , 字段名 数据类型, 字段名  数据类型, 字段名  数据类型)

例:

create table student(number varchar(256), name varchar(256), address text, QQ char(32));

2.插入数据

insert into 表名  values('字段数据''字段数据''字段数据''字段数据' );

例:

insert into student values('20200101', '张三', '广州','911683830');
insert into student values('20200102', '李四', '广州','911683831');

3.查询数据

(1)查询

select  字段名...字段名  from  表名;

说明:字段名如果是多个可以用逗号隔开,如果是所有可以用星号*

例:

select * from student ;

select name, qq from student;

(2)条件查询

select  字段名...字段名  from  表名  where 条件;

例:

select * from student where address='广州';

select * from student where address like '广%';

条件里面的where address=‘广州’; 等于号表示必须一样, 如果是模糊查询address like ‘广%’;

(3)两个条件必须同时成立(与)-and

select  字段名...字段名  from  表名  where 条件 and 条件;

例:

select * from student where address like '广%' and  QQ like '%1';

(4)两个条件只要成立一个(或)-or

select  字段名...字段名  from  表名  where 条件 or 条件;

例:

select * from student where address like '广%' or  QQ like '%1';

4. 更新数据

update 表名 set 字段1=字段1, 字段2=字段2值… where 条件表达式;

例:、

update student set qq='199999999999' where name='岳飞';

5. 删除数据

delete  from 表名;//删除整个表数据,不会删除表格

delete  from 表名  where  条件;

例:

delete from student where number='20200103';

6.查询创建表命令

.schema 表名

例:

.schema student

7.alter添加字段

alter table 表名 add column 字段 类型 (default '默认值');

例:

alter table student add column age int ;

alter table student add column sex varchar(8) default '男' ;

8.pragma 查询表结构信息

pragma table_info(表名);

例:

pragma table_info(student);

9.创建带约束数据表

创建表格的时候设置字段约束

PRIMARY KEY主键,NOT NULL不能为NULL,UNIQUE唯一,DEFAULT默认值,ID INTEGER PRIMARY KEY AUTOINCREMENT id自动增长

以此表格为例:

idnamestatusonline
1led00
2led111
3led201

(1)创建数据表

create  table  device  (id  integer primary key autoincrement,
                         name  varchar(256) unique ,
                         status int not NULL default 0,
                         online int not NULL);

创建时加入条件(表不存在是创建):

create table if not exists device(id integer primary key autoincrement, 
                                  name varchar(256) unique, 
                                  status int default 0, 
                                  online int not NULL);

(2)插入数据

insert into device values(0,'led',0,0);

当主键或不可重复的字段出现重复时,插入失败

insert into device values(0,'led',0,0);
Error: UNIQUE constraint failed: device.id
insert into device values(1,'led',0,0);
Error: UNIQUE constraint failed: device.name

指定字段(列)插入–>没有指定的就可以用默认值

insert into device(name, online) values('led2',0);
insert into device(name, online) values('led3',0);

10.删除表

drop  table  表名

例:

drop table test;
  • 14
    点赞
  • 167
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
06-01
SQLite是一种轻型、高效、自包含的SQL数据库引擎,支持大部分SQL标准,包括事务和触发器等高级特性。SQLite不需要独立的服务器进程或操作系统进程,它直接读取或写入普通的磁盘文件,因此非常适合于嵌入式设备、移动设备以及小型应用程序。 在Python中,可以使用标准库中的`sqlite3`模块来连接SQLite数据库,并执行SQL语句。`sqlite3`模块提供了一组API,可以创建数据库、创建表、插入数据、查询数据等操作。 下面是一个简单的示例代码,演示了如何使用`sqlite3`模块连接SQLite数据库,并创建一个表: ```python import sqlite3 # 连接数据库 conn = sqlite3.connect('test.db') # 创建一个表 conn.execute(''' CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL ); ''') # 关闭数据库连接 conn.close() ``` 在上面的示例中,`sqlite3.connect()`函数用于连接SQLite数据库,如果不存在则会自动创建。`conn.execute()`方法用于执行SQL语句,创建一个名为`users`的表,包含`id`、`name`和`age`三个字段。`PRIMARY KEY`、`AUTOINCREMENT`、`NOT NULL`等关键字用于设置字段的属性。最后,使用`conn.close()`方法关闭数据库连接。 除了创建表以外,`sqlite3`模块还提供了许多其他的API,例如插入数据、查询数据、更新数据、删除数据等操作。可以通过调用`conn.execute()`方法并传入对应的SQL语句来执行这些操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java.L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值