zoukankan      html  css  js  c++  java
  • 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> 
  • 相关阅读:
    linux内核中GNU C和标准C的区别
    linux内核中GNU C和标准C的区别
    Getting start with dbus in systemd (02)
    Getting start with dbus in systemd (01)
    Getting start with dbus in systemd (03)
    物理内存相关的三个数据结构
    数据类型对应字节数(32位,64位 int 占字节数)
    Linux kernel 内存
    共模电感的原理以及使用情况
    [原创]DC-DC输出端加电压会烧毁
  • 原文地址:https://www.cnblogs.com/techdreaming/p/5576110.html
Copyright © 2011-2022 走看看