天天看點

sqlite建表語句(特别是外鍵問題)

在部落格園中的原創  sqlite建表語句(特别是外鍵問題)

下面圖表示兩個表關系:點選打開連結

sqlite建表語句(特别是外鍵問題)

//表1User_invite

create table User_invite(

Invite_id INTEGER PRIMARY KEY,     //注意:這裡就代表是自動增長

user_id INTEGER, 

Invite_date DATE ,

Invite_place VARCHAR(20) NOT NULL,

Invite_kind VARCHAR(20), 

Invite_title VARCHAR(20),

Invite_other VARCHAR(50),

Invite_goodCount INTEGER,

Invite_talkCount VARCHAR(20),

Invite_enrollCount VARCHAR(20),

FOREIGN KEY (user_id ) REFERENCES User_info(user_id));  //注意這裡:寫的外鍵要寫到最後,否則會出現Error: unknown column "user_id" in foreign key definition

故還有要先執行下面的

//必須在運作時打開, 因為 預設是關閉的

PRAGMA foreign_keys = ON;

//插入語句

insert into User_invite(user_id,Invite_date,

Invite_place,Invite_kind,

Invite_title,Invite_other,

Invite_goodCount,Invite_talkCount,

Invite_enrollCount) 

values('1','2012-12-12','太原','輔導','輔導','無','1','很好','10');

//表2User_infor

create table User_info(

user_id INTEGER PRIMARY KEY,

user_name VARCHAR(50) NOT NULL ,

user_password VARCHAR(20) NOT NULL,

user_credit INTEGER, 

user_sex VARCHAR(2),

user_age INTEGER,

User_constellation VARCHAR(50),

User_state INTEGER); //線上為1,離線為0 //用數字表示幾顆星,為信用标志

//插入語句

insert into User_info(user_name,user_password,user_credit, user_sex,user_age,User_constellation,User_state) values('xiaoming','123','5','男','22','無','1');

insert into User_info(user_name,user_password,user_credit, user_sex,user_age,User_constellation,User_state) values('張三','123','2','男','20','無','1');

update mytab set name='liming' where birthday='1992-12-12';

update mytab set name='zhangsan' where birthday='1993-10-12';

update mytab set name='wangwu' where birthday='1993-02-12';

update mytab set name='xiaoming' where birthday='1993-11-12';

====進入Android中的資料庫在cmd中敲這樣的指令(注意這裡首先将模拟器打開或有真實的手機)======

adb shell 

cd data/data/org.lxh.demo/databases 

ls  ---->檢視目前的檔案 

sqlite3 xxx.db     即可

進入>sqlite 

--------------

同時也可以用 指令 “.table” 檢視已經建好的表,也可以”.schema“ 檢視表的結構

sqlite建表語句(特别是外鍵問題)

--------------

插入資料後結果如下:

sqlite建表語句(特别是外鍵問題)

繼續閱讀