玩转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 点命令导出数据库到文本文件中
sqlite3 test1.db .dump > test1.sql
相关文章