玩转SQLite2:SQLite命令行基本操作
本篇介绍SQLite的命令行基本操作
1 SQLite 点命令
SQLite 的点命令,是一些以点为开头的命令:

完整的点指令如下:
.archive ...Manage SQL archives.auth ON|OFFShow authorizer callbacks.backup ?DB? FILE备份DB数据库(默认是 “main”)到 FILE 文件.bail on|off发生错误后停止,默认为 OFF.binary on|offTurn binary output on or off. Default OFF.cd DIRECTORYChange the working directory to DIRECTORY.changes on|offShow number of rows changed by SQL.check GLOBFail if output since .testcase does not match.clone NEWDBClone 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.excelDisplay the output of next command in spreadsheet.exit ?CODE?以CODE码退出SQLite提示符.expertEXPERIMENTAL. 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 TABLECreate imposter table TABLE on index INDEX.indexes ?TABLE?显示所有索引的名称.limit ?LIMIT? ?VAL?Display or change the value of an SQLITE_LIMIT.lint OPTIONSReport potential schema issues..load FILE ?ENTRY?加载一个扩展库.log FILE|off开启或关闭日志,可以是stderr或stdout.mode MODE ?TABLE?设置输出模式.nonce STRINGDisable 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 NInvoke progress handler after every N opcodes.prompt MAIN CONTINUE替换标准提示符.quit退出 SQLite 提示符.read FILERead input from FILE.recoverRecover as much data as possible from corrupt db..restore ?DB? FILERestore content of DB (default “main”) from FILE.save FILEWrite in-memory database into FILE.scanstats on|offTurn 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 NAMEBegin 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.vfslistList all available VFSes.vfsname ?AUX?Print the name of the VFS stack.width NUM1 NUM2 ...Set minimum column widths for columnar output
例如,使用.show指令可以查看当前的各种设置:

2 SQLite 创建数据库
使用sqlite3 命令来创建数据库有两种方式
2.1 方式1:sqlite3+数据库名
例如,使用sqlite3 test1.db创建test1数据库,然后使用.databases查看数据库
.
2.2 方式2:使用.open命令
例如,使用.open test2.db创建test2数据库

2.3 将数据库导出到文件
使用 .dump 点命令导出数据库到文本文件中
1 | sqlite3 test1.db .dump > test1.sql |
也可以从生成的 testDB.sql 恢复:
1 | sqlite3 test1.db < test1.sql |

3 SQLite 创建表
可以通过CREATE TABLE语句来创建表,其基本语法为:
1 | CREATE TABLE database_name.table_name( |
例如,创建一个 COMPANY 表,ID 作为主键,NOT NULL 的约束表示在表中创建纪录时这些字段不能为 NULL:
1 | sqlite> CREATE TABLE COMPANY( |
然后可以使用 .tables命令来验证表是否已成功创建
1 | sqlite>.tables |

也可以使用.schema命令得到表的完整信息
1 | sqlite>.schema COMPANY |

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

end
-------------纸短情长 下次再见-------------
关注微信公众号,获取更多精彩~
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 码农爱学习的博客!
评论









