最簡單的資料庫sqlite3

2021-09-08 16:19:34 字數 4629 閱讀 6017

一 sqlite

簡單地說sqlite資料庫對應的就是乙個檔案(一般命名為***.db)。可以通過sqlite shell命令列來操作sqlite資料庫,也可以在其他的語言中通過api來操作sqlite資料庫。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>

.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 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> 

完! 

Sqlite3 資料庫使用

iphone本身是支援 sqlite3 資料庫的,在專案中匯入libsqlite3.dylib。並建立資料庫,在終端,建立資料庫的方式 mkdir sql 建立sql資料夾 cd sql 進入sql目錄下 sqlite3 student.sql 建立名為 student.sql的資料庫 建立表 插入...

sqlite3資料庫操作

1 開啟資料庫 1 需要制定資料庫的路徑 nsstring filepath nshomedirectory documents data.sqlite 2 建立資料庫的物件 sqlite3 qingyundb null 3 開啟命令 sqlite3 open dbfilepath utf8stri...

SQLite3資料庫操作

簡單的sqlite3語句,通過字串拼接執行資料庫操作。1.建立資料庫格式 db.execsql create table if not exists sharp id integer primary key,name varchar,level integer,high integer 其真正的有效...