SQLite 簡單使用

2021-09-06 09:05:22 字數 3053 閱讀 2895

在ubuntu 12.04下進行sqlite開發簡單例項如下:

1、 安裝sqlite3

hadron@hadron ~ $ sudo apt-get install sqlite sqlite3

2、 檢視版本號

hadron@hadron ~ $ sqlite3 -version

3、 建立test資料庫

hadron@hadron ~ $ sqlite3 test.db

sqlite version 3.7.9 2011-11-01 00:52:41

enter ".help" for instructions

enter sql statements terminated with a ";"

sqlite>

4、 檢視資料庫

sqlite> .database

seq  name             file

0    main             /home/hadron/test.db

5、 建立資料表

sqlite> create table user(id,username,password);

6、 插入資料

sqlite> insert into user(id,username,password) values(1,'abc','123');

7、 查詢資料

sqlite> select * from user;

1|abc|123

8、 退出資料庫

sqlite> .exit

9、 再次進入資料庫

hadron@hadron ~ $ sqlite3

sqlite version 3.7.9 2011-11-01 00:52:41

enter ".help" for instructions

enter sql statements terminated with a ";"

sqlite>

10、安裝視覺化工具:

hadron@hadron ~ $ sudo apt-get install sqlitebrowser

1.建立資料庫 sqlite3 mydb.db

huyf@huyf-linux:~/mydb$ sqlite3 mydb.db

sqlite version 3.7.4

enter ".help" for instructions

enter sql statements terminated with a ";"

sqlite>

就建立了乙個mydb.db資料庫。

2.建立表  

sqlite> create table cases(time varchar(20) primary key,id varchar(20));

sqlite> .tables

cases

sqlite>

上面建立了乙個表,表明cases,有兩列time,id。

.tables 檢視資料庫中現有的表,於是cases就在那兒了。

3.檢視表的屬性 .schema cases

sqlite> .schema cases

create table cases(time varchar(20) primary key,id varchar(20));

sqlite>

似乎就是建立表時的語句啊,嘿嘿

4.插入一條記錄,

sqlite> insert into cases(time,id) values ('201204111120','1234567890');

5.查詢記錄,如果需要顯示表頭,那執行.h on。

sqlite> select * from cases;

201204111120|1234567890

sqlite> .h on

sqlite> select * from cases;

time|id

201204111120|1234567890

sqlite>

6.刪除記錄

刪除全部,再查都沒有了。

sqlite> delete from cases;

sqlite> select * from cases;

sqlite>

刪除制定記錄,先多插入一條記錄,查詢發現有兩條記錄,刪除id='1234567890'的記錄,

再差,它已經不見了。

sqlite> insert into cases(time,id) values ('201204010101','0123456789');

sqlite> select * from cases;

time|id

201204111120|1234567890

201204010101|0123456789

sqlite> delete from cases where id='1234567890';

sqlite> select * from cases;

time|id

201204010101|0123456789

sqlite>

7.修改記錄

sqlite> select * from cases;

time|id

201204010101|0123456789

sqlite> update cases set time='20121223' where id='0123456789';

sqlite> select * from cases;

time|id

20121223|0123456789

sqlite>

8.最後退出

sqlite>

sqlite> .exit

huyf@huyf-linux:~/mydb$

ios簡單sqlite使用

sqlite是嵌入式的和輕量級的sql資料庫。sqlite是由c實現的。廣泛用於包括瀏覽器 支援html5的大部分瀏覽器,ie除外 ios android以及一些便攜需求的小型web應用系統。使用sqlite前的準備 使用sqlite是很多做ios開發中第一次面對c的情況,包括我。因為sqlite是...

SQLite 簡單使用(一)

sqlite 使用 新增列 alter table user add column sync state integer not null default 1 建立表 create table if not exists localuser uid integer not null primary ...

SQLite 簡單使用(二)

建立表create table if not exists localuser id integer primary key autoincrement not null,user id text not null,location text 改變表名alter table 舊表名 rename t...