Linux下SQlite3数据库--认识操作(八个步骤实现)


sqlite数据库–认识操作

(Linux网络编程)

步骤一、打开数据库

  • 打开数据库 sqlite3 xxxx.db //进入sqlite3客户端,但xxxx.db存在时,则直接打开,如果不存在,则在创建表之后,创建数据库文件。

步骤二、创建数据库文件

  • 创建:create table students(名称1 名称1类型 主键(primary key) ,名称2 名称2类型,名称3 名称3类型,名称4 名称4类型)

//创建例子: 创建一个students表,主要包括 学号(id),姓名(name),年龄(age),C语言成绩(score),主键为学号字段
//create table students(id integer primaery key,name text,age integer,score real);

步骤三、向数据库文件插入数据

  • 插入记录:insert into xxxx(数据库文件) values(x,xx,xx,xx);

    • //插入例子:insert into students values(1,‘Tom’,23,34.5);

步骤四、查询数据库文件记录

  • 方式一

    • 查询记录1:select *from xxxx(数据库文件);

      • //查询记录例子:.headers on //显示字段名
        .mode column //按照列模式
        select *from students; //查询
  • 方式二

    • 查询记录2:
      ①select 名称1 from xxxx(数据库文件); //只查看数据库文件中名称1的信息.
      ②select 名称1,名称2 from xxxx(数据库文件); //只查看数据库文件中名称1,名称2的信息 .

      • //查询记录例子:
        ①select age from students; //查询有关age的信息
        ②select name , age from students; //查询有关name 和 age 有关的信息
  • 方式三

    • 查询记录3:select *from xxxx(数据库文件) where //where子句勇于指定一个表或多个表中获取数据的条件,where后面需要接条件,对应条件表达式可以使用比较或逻辑运算符指定条件,比如< 、>、= 、LIKE 、NOT等等。

      • //查询记录2例子 :
        ①查找students表中年龄小于25岁的学员
        ②查找students表中年龄小于25并且分数大于60的学员
        –>①select *from students where age < 25;
        –>②select *from students where age < 25 and score > 60;

步骤六、更新/修改 数据中的某一个数据

  • 更新记录:update查询用于修改表中已有的记录,可以使用带有where子句的update查询来更新选定行,否则所有的行都被更新。
    update xxxx(数据库文件) set 名称1 = xxx where 名称2 = xx ; //修改名称2(主键) 中的名称1的数据

    • 例子:update students set name = ‘ben’ where id = 2; //修改id = 2的名字为’ben’

步骤七、删除记录

  • 删除记录:使用delete字段,具体的语法如下。
    delete from xxxx(数据库文件) where xxx(数据库文件下的数据)

    • //例子:delete from students where name = ‘Mark’; //删除name = ‘Mark’的记录

步骤八、删除数据库文件中的数据

删除数据库表:使用delete字段,具体的语法如下。
drop table xxxx(需要删除的数据库文件)

  • //例子:drop table students

猜你喜欢

转载自blog.csdn.net/weixin_50019298/article/details/114371074
今日推荐