• sqlite 的基本使用1


    mac 下自带的sqlite3 直接在终端键入 sqlite3 即进入 sqlite的交互界面

    1,创建数据库

      sqlite3 命令 被用来创建新的数据库 比如sqlite3 mydb,即创建了一个mydb的数据库

    bogon:db lining$ sqlite3 mydb
    SQLite version 3.8.10.2 2015-05-20 18:17:19
    Enter ".help" for usage hints.
    sqlite> 

      进入交互界面之后,如何退出,可以键入.quit退出交互界面 

    SQLite version 3.8.10.2 2015-05-20 18:17:19
    Enter ".help" for usage hints.
    sqlite> 
    sqlite> 
    sqlite> 
    sqlite> .quit
    bogon:db lining$ 

    2,创建数据表

      数据库现在创建好了,可以开始创建数据表了,create table 语句被用来创建数据表

    sqlite> create table student(
       ...> id int primary key not null,
       ...> name char(20) not null,
       ...> age int not null);
    sqlite> 

      同时,我们可以用.tables 命令查看表是否成功创建

    sqlite> .tables
    student

      可以使用.schema table_name 查看表结构的完整信息

    sqlite> .schema student
    CREATE TABLE student(
    id int primary key not null,
    name char(20) not null,
    age int not null);
    sqlite> 

    3,删除数据表

      如果我们想要删除一张数据表怎么做呢,可以使用drop table_name命令操作,同样,像刚才那样使用.tables 查看是否表删除成功

    sqlite> drop table student;
    sqlite> .tables
    sqlite> 

    4,数据表的插入操作

      数据表中插入一项,使用insert into 命令,该命令有两种写法,

      一种是 insert into table_name values(,,,);

      例子:

    sqlite> insert into student values(2,"bb",12);
    sqlite> select * from student;
    1|aa|23
    2|bb|12
    sqlite> 

      一种是 insert into table_name(,,,) values(,,,);  

    sqlite> insert into student (id,name,age)
       ...> values(3,"cc",45);
    sqlite> select * from student;
    1|aa|23
    2|bb|12
    3|cc|45
    sqlite> 

    5,数据表的选择

      从表中获取信息的一个最直接的方法就是select * from table_name,上面例子已经给出。这里显示的不好看,

      .head on 命令 开启输出表头,.mode column 命令,格式化输出列。这样就好看多了

    sqlite> select * from student
       ...> ;
    id          name        age       
    ----------  ----------  ----------
    1           aa          23        
    2           bb          12        
    3           cc          45        
    sqlite> 

      同时可以自由的设置列的宽度,还是很简单方便的。

    sqlite> .width 5,10,20
    sqlite> select * from student;
    id     name        age       
    -----  ----------  ----------
    1      aa          23        
    2      bb          12        
    3      cc          45        
    sqlite> 
  • 相关阅读:
    msgs no .h file
    我们为之奋斗过的C#之---简单的库存管理系统
    我们为之奋斗过的C#-----C#的一个简单理解
    套接字之sendto系统调用
    套接字之msghdr结构
    套接字之select系统调用
    套接字之close系统调用
    TCP之listen&backlog
    套接字之相关系统调用的调用流程
    套接字之shutdown系统调用
  • 原文地址:https://www.cnblogs.com/techdreaming/p/5576110.html
Copyright © 2020-2023  润新知