SQLite数据库的挂接及常用命令(转自:http://blog.csdn.net/windone0109/article/details/5514948)

来源:互联网 发布:usb网卡无法连接网络 编辑:程序博客网 时间:2024/05/16 05:48

安装:

官方网站下载最新的sqlite版本

官方网站: http://www.sqlite.org/

下载地址为: http://www.sqlite.org/download.html

官方的下载页面提供了很多版本的下载…这里介绍一下;

Source Code: 源代码版本的下载

Documentation:  相关文档

Precompiled Binaries for Linux / Precompiled Binaries For Mac OS X /

Precompiled Binaries For Windows

Linux/Mac/Win版本的预编译版本


如果你只是数据库的用户那么下载已经编译好的版本即可! 这里我是Win用户,下载的文件为: sqlite3.exe

将这个文件放到一个目录中,这样就完成了全部的sqlite数据库的安装!

 

新建:

在命令提示符下($为shell提示号), 测试的sqlite3.exe路径为e:/sqlite3/sqlite3.exe

网上也提供了很多方法,但好象都有点问题,经过摸索结论为以下几种方法,但不确定是否受操作系统系统影响!

生成数据库文件后挂接 
$> sqlite3.exe test.db ;

$> sqlite3.exe test.db

直接挂接生成

$> sqlite3.exe test.db;

选择性挂接 挑选已经存在的数据文件挂接 
$> sqlite3.exe test.db 或者

$> sqlite3.exe e:/sqlite3/test.db

挂接好数据库后,就可以通过命令行的形式对所挂接的数据库操作了!


   Cmd 进入命令行
   1)
   创建数据库文件:
   >SQLite3 d:/test.db 回车
   就生成了一个test.db在d盘。
   这样同时也SQLite3挂上了这个test.db

   2) 
   用.help可以看看有什么命令
   >.help 回车即可

   3)可以在这里直接输入SQL语句创建表格 用;结束,然后回车就可以看到了

   4)看看有创建了多少表
   >.tables

   5)看表结构
   >.schema 表名

   6)看看目前挂的数据库
   >.database

   7)如果要把查询输出到文件
   >.output 文件名
   > 查询语句;
   查询结果就输出到了文件c:/query.txt 
   把查询结果用屏幕输出
   >.output stdout


   8)把表结构输出,同时索引也会输出
     .dump 表名
   9)退出
   >.exit 或者.quit

 

sqlite官方帮助提示信息:

sqlite> .help.help.bail ON|OFF           Stop after hitting an error.  Default OFF.databases             List names and files of attached databases.dump ?TABLE? ...      Dump the database in an SQL text format.echo ON|OFF           Turn command echo on or off.exit                  Exit this program.explain ON|OFF        Turn output mode suitable for EXPLAIN on or off..header(s) 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 on TABLE.load FILE ?ENTRY?     Load an extension library.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      Print STRING in place of NULL values.output FILENAME       Send output to FILENAME.output stdout         Send output to the screen.prompt MAIN CONTINUE  Replace the standard prompts.quit                  Exit this program.read FILENAME         Execute SQL in FILENAME.schema ?TABLE?        Show the CREATE statements.separator STRING      Change separator used by output mode and .import.show                  Show the current values for various settings.tables ?PATTERN?      List names of tables matching a LIKE pattern.timeout MS            Try opening locked tables for MS milliseconds.timer ON|OFF          Turn the CPU timer measurement on or off.width NUM NUM ...     Set column widths for "column" mode


 

 

它支持大部分的SQL命令,这是一个列表:

  • ATTACH DATABASE
  • BEGIN TRANSACTION
  • comment
  • COMMIT TRANSACTION
  • COPY
  • CREATE INDEX
  • CREATE TABLE
  • CREATE TRIGGER
  • CREATE VIEW
  • DELETE
  • DETACH DATABASE
  • DROP INDEX
  • DROP TABLE
  • DROP TRIGGER
  • DROP VIEW
  • END TRANSACTION
  • EXPLAIN
  • expression
  • INSERT
  • ON CONFLICT clause
  • PRAGMA
  • REPLACE
  • ROLLBACK TRANSACTION
  • SELECT
  • UPDATE 

    它还提供了虚拟机用于处理sql语句,这是一个很有趣的东西。支持事务功能。

  • 原创粉丝点击