Android下SQLite数据库编程学习系列之一---熟悉SQLite及命令行操作

Android下SQLite数据库编程学习系列之一---熟悉SQLite


SQLite介绍

SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至今已经有12个年头,SQLite也迎来了一个版本 SQLite 3已经发布。

在Ubuntu下安装SQLite

在Linux shell下我们可以通过:

root@long-desktop:~# apt-get install sqlite3

命令来安装SQLite了!

安装之后:

root@long-desktop:/work/code/database# sqlite3 MyDemo.db
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.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
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.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.
                         With no args, it turns EXPLAIN on.
.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
                         If TABLE specified, only show indices for tables
                         matching LIKE pattern TABLE.
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.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
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.vfsname ?AUX?         Print the name of the VFS stack
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off
sqlite> 

对帮助文件的说明:

.backup ?DB? FILE		备份数据库到文件,如果没有指定数据库名称则备份默认数据库(main)
.bail ON|OFF  			Stop after hitting an error.  Default OFF
.databases    			显示所有数据库
.dump ?TABLE? ...  		Dump the database in an SQL text format 
				If TABLE specified, only dump tables ma LIKE pattern TABLE.
 
.echo ON|OFF			回显开关
.exit				退出
.explain ?ON|OFF?		Turn output mode suitable for EXPLAIN on With no args, it turns EXPLAIN on.
 
.header(s) ON|OFF	  是否显示列名
.help 	显示帮助
.import FILE TABLE	从指定文件向指定的表导入数据
.indices ?TABLE?	如果不指定表名,显示所有表的索引
.load FILE ?ENTRY?	Load an extension library
.log FILE|off    	Turn logging on or off.  FILE can be stde
.mode MODE ?TABLE?  	设置输出模式:
csv      Comma-separated values
column   Left-aligned columns.  (See .w
html     HTML <table> code
insert   SQL insert statements for TABL
line     One value per line
list     Values delimited by .separator
tabs     Tab-separated values
tcl      TCL list elements
 
.nullvalue STRING	使用指定字符串替换数据中的NUll值
.output FILENAME	输出到文件
.output stdout	输出到屏幕
.prompt MAIN CONTINUE	改变命令提示(默认为sqlite)
.quit	退出
.read FILENAME	Execute SQL in FILENAME
.restore ?DB? FILE	如果不指定数据库名称(默认为main)则关闭现有连接,打开一个新数据库连接;如果指定了main则用新数据库覆盖当前数据库的内容
.schema ?TABLE?	如果不指定表名则显示所有表的信息(创建语句、插入语句、视图、索引等)
.separator STRING	修改查询结果中列之间的分隔符
.show  	显示当前设置
.stats ON|OFF	打开或关闭改变统计
.tables ?TABLE?	列出当前数据库中的表名
.timeout MS	打开锁定的表的超时时间
.vfsname ?AUX?	Print the name of the VFS stack
.width NUM1 NUM2 ...	设置输出的列的宽度
.timer ON|OFF	Turn the CPU timer measurement on or off

SQLite初步操作:

在Linux Shell上我们可以通过:

// 创建表
sqlite> create index name_index_File on File(Name);
// 插入数据
sqlite> insert into File values(001, "Jessica", 2011, "USA");sqlite> insert into File values(002, "Jake:", 2005, "China");
// 查询表格
sqlite> select * from File
   ...> ;
1|Jessica|2011|USA
2|Jake:|2005|China
56|Sarah|2008|German
sqlite> 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值