天天看点

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建表语句(特别是外键问题)

继续阅读