萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mssql數據庫 >> SQLite數據庫操作實踐

SQLite數據庫操作實踐

 SQLite數據庫操作實踐
SQLite存儲類型
null - 空值 
integer - 有符號整數,自增列定義 id integer primary key autoincrement
real - 浮點數,存儲為8-byte IEEE浮點數 
text - 文本字符串 使用數據庫編碼(UTF-8, UTF-16BE 或 UTF-16LE)進行存儲
blob - 二進制數據,如圖片、聲音等等 
也可以接受其它數據類型例如datetime,varchar,nvarchar,decimal,float,double,bigint 等等

Last login: Tue Jan 7 19:05:33 2014 from 192.168.10.1
[myth@host10 ~]$ su
Password: 
[root@host10 myth]# pwd //顯示當前工作目錄

/home/myth

進入SQLite命令行創建數據庫並且打開數據庫

[root@host10 myth]# sqlite3 bookstore.db 
SQLite version 3.6.20
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .databases
seq name file 
--- --------------- ----------------------------------------------------------
0 main /home/myth/bookstore.db 
sqlite> 
sqlite> .quit //退出SQLite命令行環境

[root@host10 myth]#

打開數據庫

[root@host10 myth]# sqlite3 bookstore.db //如果數據庫已經存在了,則打開數據庫
SQLite version 3.6.20
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.
.genfkey ?OPTIONS? Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source 
distribution for further information.
.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
.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
.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
.width NUM NUM ... Set column widths for "column" mode
.timer ON|OFF Turn the CPU timer measurement on or off

sqlite>

創建表

sqlite> create table books(id integer primary key, title nvarchar(50),pubdate datetime); 
sqlite> create table publishers(id integer primary key, name varchar(20)); 
sqlite> .tables //查看數據庫中存在的表
books publishers
sqlite> .schema books //顯示表結構
CREATE TABLE books(id integer primary key, title nvarchar(50),pubdate datetime);
sqlite> 
sqlite> alter table books add column publisher text not null default '' collate nocase; //增加表字段
sqlite> .schema books //顯示表結構
CREATE TABLE books(id integer primary key, title nvarchar(50),pubdate datetime, publisher text not null default '' collate nocase);
sqlite> 
sqlite> alter table publishers rename to authors; //修改表名
sqlite> .tables //查看數據庫中存在的表
authors books 

sqlite>

修改字段名稱

//修改字段名稱和數據類型(刪除字段也類似),更簡單的做法:如果沒有重要數據,可以直接刪除表重新創建

sqlite> alter table authors rename to authorsOld;
sqlite> create table authors (id integer primary key, author nvarchar(20));
sqlite> insert into authors select id, name from authorsOld;
sqlite> drop table authorsOld;
sqlite> .schema authors
CREATE TABLE authors (id integer primary key, author nvarchar(20));

//還可以把數據導出成insert語句,然後刪除表,重新創建表,執行所有insert語句添加數據

顯示數據庫所有表名稱

sqlite> select name from sqlite_master where type='table' order by name; //顯示數據庫所有表名稱,sqlite_master表是只讀的
authors
books
sqlite> select name from 
...> (SELECT * from sqlite_master union all select * from sqlite_temp_master)
...> where type='table' order by name; //顯示數據庫所有表名稱(永久和臨時)
authors
books

sqlite>

刪除表

sqlite> drop table authors; 
sqlite> .tables //查看數據庫中存在的表
books
sqlite>

往表中插入數據
sqlite> insert into books(id,title,pubdate,publisher) values(1,'交換機.路由器.防火牆','2007-08-28','電子工業出版社');
sqlite> insert into books(id,title,pubdate,publisher) values(2, '網絡硬件搭建與配置實踐','2007-01-20','電子工業出版社');
sqlite> 
sqlite> insert into books(title,pubdate,publisher) values('英語沙龍 ENGLISH SALON 實戰版','2005-12-31','世界知識出版社');
sqlite> insert into books(title,pubdate,publisher) values('英語沙龍 ENGLISH SALON 朗讀版','2006-12-20','世界知識出版社');
sqlite> 
sqlite> insert into books 
...> select 100,'軟件項目管理理論與案例分析','2007-08-30','中國電力出版社' union all 
...> select 105,'初級會計電算化','2006-03-27','中國財政經濟出版社';
sqlite> 
sqlite> insert into books(id,title,pubdate,publisher) 
...> select 300,'供應鏈管理 ------戰略、規劃與運營','2003-02-28','社會科學文獻出版社' union all
...> select 301,'UML實戰教程 ------面向.NET開發人員','2006-12-31','清華大學出版社' union all
...> select 302,'Project 2003 企業項目管理 快譯通','2007-02-28','人民郵電出版社';
sqlite> 
sqlite> select * from books;
1|交換機.路由器.防火牆|2007-08-28|電子工業出版社
2|網絡硬件搭建與配置實踐|2007-01-20|電子工業出版社
3|英語沙龍 ENGLISH SALON 實戰版|2005-12-31|世界知識出版社
4|英語沙龍 ENGLISH SALON 朗讀版|2006-12-20|世界知識出版社
100|軟件項目管理理論與案例分析|2007-08-30|中國電力出版社
105|初級會計電算化|2006-03-27|中國財政經濟出版社
300|供應鏈管理 ------戰略、規劃與運營|2003-02-28|社會科學文獻出版社
301|UML實戰教程 ------面向.NET開發人員|2006-12-31|清華大學出版社

copyright © 萬盛學電腦網 all rights reserved