Sqlite命令行基本操作

Sqlite命令行基本操作

SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C程序库中。

与许多其它数据库管理系统不同,SQLite不是一个客户端/服务器结构的数据库引擎,而是被集成在用户程序中。

1、进入命令行环境:sqlite3

打开一个控制台窗口,输入 sqlite3 回车,这时你就进入了 SQLite 命令行环境,如图

它显示了版本号,并告诉你每一条 SQL 语句必须用分号;结尾。

2、命令行帮助:.help

在命令行环境下输入 .help 回车,显示所有可使用的命令以及这些命令的帮助。注意:所有的命令开头都是一个点。

 
  1. .backup ?DB? FILE Backup DB (default "main") to FILE

  2. .bail ON|OFF Stop after hitting an error. Default OFF

  3. .databases List names and files of attached databases

  4. .dump ?TABLE? ... Dump the database in an SQL text format

  5. If TABLE specified, only dump tables matching

  6. LIKE pattern TABLE.

  7. .echo ON|OFF Turn command echo on or off

  8. .exit Exit this program

  9. .explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.

  10. With no args, it turns EXPLAIN on.

  11. .header(s) ON|OFF Turn display of headers on or off

  12. .help Show this message

  13. .import FILE TABLE Import data from FILE into TABLE

  14. .indices ?TABLE? Show names of all indices

  15. If TABLE specified, only show indices for tables

  16. matching LIKE pattern TABLE.

  17. .load FILE ?ENTRY? Load an extension library

  18. .log FILE|off Turn logging on or off. FILE can be stderr/stdout

  19. .mode MODE ?TABLE? Set output mode where MODE is one of:

  20. csv Comma-separated values

  21. column Left-aligned columns. (See .width)

  22. html HTML <table> code

  23. insert SQL insert statements for TABLE

  24. line One value per line

  25. list Values delimited by .separator string

  26. tabs Tab-separated values

  27. tcl TCL list elements

  28. .nullvalue STRING Print STRING in place of NULL values

  29. .output FILENAME Send output to FILENAME

  30. .output stdout Send output to the screen

  31. .prompt MAIN CONTINUE Replace the standard prompts

  32. .quit Exit this program

  33. .read FILENAME Execute SQL in FILENAME

  34. .restore ?DB? FILE Restore content of DB (default "main") from FILE

  35. .schema ?TABLE? Show the CREATE statements

  36. If TABLE specified, only show tables matching

  37. LIKE pattern TABLE.

  38. .separator STRING Change separator used by output mode and .import

  39. .show Show the current values for various settings

  40. .stats ON|OFF Turn stats on or off

  41. .tables ?TABLE? List names of tables

  42. If TABLE specified, only list tables matching

  43. LIKE pattern TABLE.

  44. .timeout MS Try opening locked tables for MS milliseconds

  45. .width NUM1 NUM2 ... Set column widths for "column" mode

  46. .timer ON|OFF Turn the CPU timer measurement on or off

  47. sqlite>

3、退出命令行环境
.quit 或者 .exit 都可以退出。

创建一个新的数据库:sqlite3 test.db
打开一个已经存在的数据库:sqlite3 已经存在的文件

创建一个新数据库和打开一个已经存在的数据库命令是一模一样的,如果文件在当前目录下不存在,则新建;如果存在,则打开。

(此时,数据库文件并没有存在)

创建一个表:

CREATE TABLE Books(Id integer PRIMARY KEY, Title text, Author text, Isbn text default 'not available');

插入数据:

 
  1. INSERT INTO Books VALUES(1, 'War and Peace', 'Leo Tolstoy', '978-0345472403');

  2.  
  3. INSERT INTO Books VALUES(2, 'The Brothers Karamazov', 'Fyodor Dostoyevsky', '978-0486437910');

  4.  
  5. INSERT INTO Books VALUES(3, 'Crime and Punishment', 'Fyodor Dostoyevsky', '978-1840224306');

或者将以上sql命令保存在文件中,使用.read *.sql进行导入

test.sql

 
  1. BEGIN TRANSACTION;

  2.  
  3. CREATE TABLE Books(Id integer PRIMARY KEY, Title text, Author text, Isbn text default 'not available');

  4.  
  5. INSERT INTO Books VALUES(1, 'War and Peace', 'Leo Tolstoy', '978-0345472403');

  6.  
  7. INSERT INTO Books VALUES(2, 'The Brothers Karamazov', 'Fyodor Dostoyevsky', '978-0486437910');

  8.  
  9. INSERT INTO Books VALUES(3, 'Crime and Punishment', 'Fyodor Dostoyevsky', '978-1840224306');

  10.  
  11. COMMIT;

列出数据表: .tables

显示数据库结构: .schema

格式化输出:

猜你喜欢

转载自blog.csdn.net/zhujialiang18/article/details/81536115