最簡單的數據庫sqlite3



一 sqlite

簡單地說SQLite數據庫對應的就是一個文件(一般命名為xxx.db)。可以通過sqlite shell命令行來操作sqlite數據庫,也可以在其他的語言中通過api來操作sqlite數據庫。sqlite數據庫的訪問甚至不需要用戶名和密碼。

 

在windows上可以下載:sqlite-shell-win32-x86-3071300.zip , 其他平台可以下載對應的shell命令行。

 

幫助:

 

d:\sqlite_test>sqlite3.exe test.db
SQLite version 3.7.13 2012-06-11 02:05:22
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
.trace FILE|off        Output each SQL statement as it is run
.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

1)打開或創建sqlite數據庫

 

d:\sqlite_test>sqlite3.exe test.db
SQLite version 3.7.13 2012-06-11 02:05:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> 

 

 

2) 創建表及插入數據

sqlite> create table mytable(name varchar(10), age smallint);
sqlite> insert into mytable values("itech", 5);
sqlite> insert into mytable values("jason", 10);
sqlite> select * from mytable;
itech|5
jason|10

sqlite> 

 

3)查看表及表的schema

sqlite> .tables
mytable
sqlite> .schema mytable
CREATE TABLE mytable(name varchar(10), age smallint);

sqlite> 

 

4)數據庫導出為ascii的sql文件 + 重新導入到新的數據庫

d:\sqlite_test>echo .dump | sqlite3 test.db > test.db.dump 

d:\sqlite_test>type test.db.dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE mytable(name varchar(10), age smallint);
INSERT INTO "mytable" VALUES('itech',5);
INSERT INTO "mytable" VALUES('jason',10);
COMMIT;
 

 

d:\sqlite_test>type test.db.dump | sqlite3 test2.db
d:\sqlite_test>sqlite3 test2.db
SQLite version 3.7.13 2012-06-11 02:05:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
mytable
sqlite>

 

 

5)格式化輸出

sqlite> .mode column
sqlite> .header on
sqlite> select * from mytable;
name        age
----------  ----------
itech       5
jason       10

sqlite> 

 

完! 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM