玩转SQLite2:SQLite命令行基本操作

本篇介绍SQLite的命令行基本操作

1 SQLite 点命令

SQLite 的点命令,是一些以点为开头的命令:

玩转SQLite2:SQLite命令行基本操作

完整的点指令如下:

  • .archive ... Manage SQL archives
  • .auth ON|OFF Show authorizer callbacks
  • .backup ?DB? FILE 备份DB数据库(默认是 “main”)到 FILE 文件
  • .bail on|off 发生错误后停止,默认为 OFF
  • .binary on|off Turn binary output on or off. Default OFF
  • .cd DIRECTORY Change the working directory to DIRECTORY
  • .changes on|off Show number of rows changed by SQL
  • .check GLOB Fail if output since .testcase does not match
  • .clone NEWDB Clone data into NEWDB from the existing database
  • .connection [close] [#] Open or close an auxiliary database connection
  • .databases 列出数据库的名称及其所依附的文件
  • .dbconfig ?op? ?val? List or change sqlite3_db_config() options
  • .dbinfo ?DB? Show status information about the database
  • .dump ?OBJECTS? 以 SQL 文本格式转储数据库
  • .echo on|off 开启或关闭 echo 命令
  • .eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN
  • .excel Display the output of next command in spreadsheet
  • .exit ?CODE? 以CODE码退出SQLite提示符
  • .expert EXPERIMENTAL. Suggest indexes for queries
  • .explain ?on|off|auto? 开启或关闭适合于 EXPLAIN 的输出模式,默认是:auto
  • .filectrl CMD ... Run various sqlite3_file_control() operations
  • .fullschema ?--indent? Show schema and the content of sqlite_stat tables
  • .headers on|off 开启或关闭头部显示
  • .help ?-all? ?PATTERN? 显示帮助
  • .import FILE TABLE 导入来自 FILE 文件的数据到 TABLE 表中
  • .imposter INDEX TABLE Create imposter table TABLE on index INDEX
  • .indexes ?TABLE? 显示所有索引的名称
  • .limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT
  • .lint OPTIONS Report potential schema issues.

  • .load FILE ?ENTRY? 加载一个扩展库

  • .log FILE|off 开启或关闭日志,可以是stderr或stdout
  • .mode MODE ?TABLE? 设置输出模式
  • .nonce STRING Disable safe mode for one command if the nonce matches
  • .nullvalue STRING 在 NULL 值的地方输出 STRING 字符串
  • .once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE
  • .open ?OPTIONS? ?FILE? 关闭存在的数据库或重新打开文件
  • .output ?FILE? Send output to FILE or stdout if FILE is omitted
  • .parameter CMD ... Manage SQL parameter bindings
  • .print STRING... 逐字地输出 STRING 字符串
  • .progress N Invoke progress handler after every N opcodes
  • .prompt MAIN CONTINUE 替换标准提示符
  • .quit 退出 SQLite 提示符
  • .read FILE Read input from FILE
  • .recover Recover as much data as possible from corrupt db.

  • .restore ?DB? FILE Restore content of DB (default “main”) from FILE

  • .save FILE Write in-memory database into FILE
  • .scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off
  • .schema ?PATTERN? Show the CREATE statements matching PATTERN
  • .selftest ?OPTIONS? Run tests defined in the SELFTEST table
  • .separator COL ?ROW? Change the column and row separators
  • .session ?NAME? CMD ... Create or control sessions
  • .sha3sum ... Compute a SHA3 hash of database content
  • .shell CMD ARGS... Run CMD ARGS… in a system shell
  • .show 显示各种设置的当前值
  • .stats ?ARG? 开启或关闭统计
  • .system CMD ARGS... Run CMD ARGS… in a system shell
  • .tables ?TABLE? List names of tables matching LIKE pattern TABLE
  • .testcase NAME Begin redirecting output to ‘testcase-out.txt’
  • .testctrl CMD ... Run various sqlite3_test_control() operations
  • .timeout MS 尝试打开锁定的表 MS 毫秒
  • .timer on|off 开启或关闭SQL定时器
  • .trace ?OPTIONS? Output each SQL statement as it is run
  • .vfsinfo ?AUX? Information about the top-level VFS
  • .vfslist List all available VFSes
  • .vfsname ?AUX? Print the name of the VFS stack
  • .width NUM1 NUM2 ... Set minimum column widths for columnar output

例如,使用 .show指令可以查看当前的各种设置:

玩转SQLite2:SQLite命令行基本操作

; 2 SQLite 创建数据库

使用sqlite3 命令来创建数据库有两种方式

2.1 方式1:sqlite3+数据库名

例如,使用 sqlite3 test1.db创建test1数据库,然后使用 .databases查看数据库

玩转SQLite2:SQLite命令行基本操作

2.2 方式2:使用 .open 命令

例如,使用 .open test2.db创建test2数据库

玩转SQLite2:SQLite命令行基本操作

; 2.3 将数据库导出到文件

使用 .dump 点命令导出数据库到文本文件中

sqlite3 test1.db .dump > test1.sql

也可以从生成的 testDB.sql 恢复:

sqlite3 test1.db < test1.sql

玩转SQLite2:SQLite命令行基本操作

3 SQLite 创建表

可以通过 CREATE TABLE语句来创建表,其基本语法为:

CREATE TABLE database_name.table_name(
   column1 datatype  PRIMARY KEY(one or more columns),
   column2 datatype,
   column3 datatype,
   .....

   columnN datatype,
);

例如,创建一个 COMPANY 表,ID 作为主键,NOT NULL 的约束表示在表中创建纪录时这些字段不能为 NULL:

sqlite> CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

然后可以使用 .tables命令来验证表是否已成功创建

sqlite>.tables
COMPANY

玩转SQLite2:SQLite命令行基本操作

也可以使用 .schema命令得到表的完整信息

sqlite>.schema COMPANY
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

玩转SQLite2:SQLite命令行基本操作

最后将数据库导出到.sql文件查看:

玩转SQLite2:SQLite命令行基本操作

Original: https://blog.csdn.net/hbsyaaa/article/details/121917129
Author: 码农爱学习
Title: 玩转SQLite2:SQLite命令行基本操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/817109/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球