sqlite基本使用方法

葛炯
2023-12-01
  1. 新建(打开)数据库文件
    sqlite3 mydata.db # 指定数据库文件启动sqlite
    .open mydata.db   # 使用.open命令打开打开数据库文件

  2. 帮助信息
    sqlite> .help
    .backup ?DB? FILE      Backup DB (default "main") to FILE
    .bail on|off           Stop after hitting an error.  Default OFF
    .clone NEWDB           Clone data into NEWDB from the existing database
    .databases             List names and files of attached databases
    .dump ?TABLE? ...      Dump the database in an SQL text format
                             If TABLE specified, only dump tables matching
                             LIKE pattern TABLE.
    .echo on|off           Turn command echo on or off
    .eqp on|off            Enable or disable automatic EXPLAIN QUERY PLAN
    .exit                  Exit this program
    .explain ?on|off?      Turn output mode suitable for EXPLAIN on or off.
                             With no args, it turns EXPLAIN on.
    .fullschema            Show schema and the content of sqlite_stat tables
    .headers on|off        Turn display of headers on or off
    .help                  Show this message
    .import FILE TABLE     Import data from FILE into TABLE
    .indices ?TABLE?       Show names of all indices
                             If TABLE specified, only show indices for tables
                             matching LIKE pattern TABLE.
    .load FILE ?ENTRY?     Load an extension library
    .log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
    .mode MODE ?TABLE?     Set output mode where MODE is one of:
                             csv      Comma-separated values
                             column   Left-aligned columns.  (See .width)
                             html     HTML <table> code
                             insert   SQL insert statements for TABLE
                             line     One value per line
                             list     Values delimited by .separator string
                             tabs     Tab-separated values
                             tcl      TCL list elements
    .nullvalue STRING      Use STRING in place of NULL values
    .once FILENAME         Output for the next SQL command only to FILENAME
    .open ?FILENAME?       Close existing database and reopen FILENAME
    .output ?FILENAME?     Send output to FILENAME or stdout
    .print STRING...       Print literal STRING

  3. 数据库信息
    sqlite> .databases
    seq  name             file
    ---  ---------------  -------------------------------------
    0    main             D:\tool\mydata.db

  4. 表列表
    sqlite> .table
    book
    sqlite>

  5. 表信息
    sqlite> .schema book
    CREATE TABLE "book"(name TEXT, price float, SN TEXT PRIMARY KEY);
    sqlite> .fullschema
    CREATE TABLE "book"(name TEXT, price float, SN TEXT PRIMARY KEY);
    /* No STAT tables available */
    sqlite> .schema
    CREATE TABLE "book"(name TEXT, price float, SN TEXT PRIMARY KEY);
    sqlite>


  6. 显示设置选项
    sqlite> .show
         echo: off
          eqp: off
      explain: off
      headers: off
         mode: list
    nullvalue: ""
       output: stdout
    separator: "|" "\r\n"
        stats: off
        width:
    sqlite>


  7. 创建表
    sqlite> CREATE TABLE staff(NO TEXT PRIMARY KEY, name VARCHAR(32), age int);
    sqlite> .table
    book   staff
    sqlite>

  8. 插入数据记录到表
    sqlite> INSERT INTO staff VALUES("2014001", "马化腾", 3);
    sqlite> INSERT INTO staff VALUES("2014002", "马云", 4);;
    sqlite> INSERT INTO staff VALUES("2014002", "李彦宏", 6);;
    Error: UNIQUE constraint failed: staff.NO
    sqlite> INSERT INTO staff VALUES("2014003", "李彦宏", 6);
    sqlite> INSERT INTO staff VALUES("2014004", "胡夸风", 6);
    sqlite> INSERT INTO staff VALUES("2014006", "窦思涡", 11);
    sqlite> select * from staff
       ...> ;
    2014001|马化腾|3
    2014002|马云|4
    2014003|李彦宏|6
    2014004|胡夸风|6
    2014006|窦思涡|11
    sqlite>

  9. 查看表中记录
    sqlite> select * from staff;
    2014001|马化腾|3
    2014002|马云|4
    2014003|李彦宏|6
    2014004|胡夸风|6
    2014006|窦思涡|11
    sqlite> select * from staff where age > 6;
    2014006|窦思涡|11
    sqlite> select * from staff where age >= 6;
    2014003|李彦宏|6
    2014004|胡夸风|6
    2014006|窦思涡|11
    sqlite> select * from staff where age >= 6 order by name;
    2014004|胡夸风|6
    2014003|李彦宏|6
    2014006|窦思涡|11
    sqlite> select * from staff where age >= 6 order by namec desc;
    Error: no such column: namec
    sqlite> select * from staff where age >= 6 order by name desc;
    2014006|窦思涡|11
    2014003|李彦宏|6
    2014004|胡夸风|6
    sqlite> select AVG(age) from staff;
    6.0
    sqlite>

  10. 更新表中记录
    sqlite> update staff set age=age+10;
    sqlite> select * from staff;
    2014001|马化腾|13
    2014002|马云|14
    2014003|李彦宏|16
    2014004|胡夸风|16
    2014006|窦思涡|21
    sqlite> select * from staff where age < 15;
    2014001|马化腾|13
    2014002|马云|14
    sqlite> update staff set age=age+10 where age < 15;
    sqlite> select * from staff;
    2014001|马化腾|23
    2014002|马云|24
    2014003|李彦宏|16
    2014004|胡夸风|16
    2014006|窦思涡|21
    sqlite>

  11. 删除表中记录
    sqlite> select * from staff;
    2014001|马化腾|23
    2014002|马云|24
    2014003|李彦宏|16
    2014004|胡夸风|16
    2014006|窦思涡|21
    2014113|will delete|23
    sqlite> delete from staff where NO='2014113';
    sqlite> select * from staff;
    2014001|马化腾|23
    2014002|马云|24
    2014003|李彦宏|16
    2014004|胡夸风|16
    2014006|窦思涡|21
    sqlite>

 类似资料: