sqlite数据库常用操作

基于Sqlite数据库的一些常用的操作语句

1、新建数据表

  • 新建一个名字为testtable的数据表,分成a,b,c 3列内容:
    create table testtable (a int primary key , b varchar , c varchar);

2、插入数据内容

1)按顺序依次插入数据

  • insert into testtable values (1,2,3);
  • insert into testtable values (2,‘str1’,‘str2’);

2)选择列插入数据

  • insert into testtable (a,c) values (3,‘str3’);

3、删除数据内容

1)清空全部内容

  • delete from testtable;

2)删除一行内容

  • delete from testtable where id=1;

3)删除一列内容

sqlite不支持直接删除列,可进行如下操作:
法一:

  • 清空某一列的内容:update testtable set c = null;

法二:

  • 在原来表的基础上新建一个表(表名不能相同):create table testtable2 as select a,c from testtable;
  • 删除原来的那个表:drop table testtable;
  • 给新建的这个表改名:alter table testtable2 rename to testtable;

4、删除数据表

  • drop table testtable;

5、数据表重命名

  • alter table testtable rename to testtable2;

6、修改列名称

例如想要将testtable中的a列名称修改为a1列,网络上有一种方法,使用alter table testtable rename column a to a1来修改a列名称,但实际操作发现有问题无法实现,考虑使用新方法:

  • 新建一个数据表:(新建时想要修改的列名称已经修改)
    create table testtable2 (a1 int primary key , b varchar , c varchar);
  • 使用insert将原来的数据全部插入到新建的表:
    insert into testtable select a,b,c from testtable;
  • 删除原来的表:
    drop table testtable;
  • 给新建的表重命名:
    alter table testtable2 rename to testtable;

7、修改列的数据类型

  • SQLite的ALTER TABLE命令在3.25.0版本之前并不支持修改列的数据类型,想要修改数据类型,参照第6点的修改列名称的步骤。
  • 如果sqlite版本在3.25.0以上,参照语句:
    alter table testtable alter column a type text;

8、修改指定数据

  • update testtable set c=“111” where a=2;

9、替换符合条件的内容

  • update 数据表名 set 列名=replace(列名,‘原内容’,‘新内容’);
  • update testtable set c = replace(c,‘111’,‘222’);

10、插入新列

  • alter table 表名 add column 列名 数据类型;
  • alter table testtable add column d int;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值