详细使用案例可以查看 GitHub
简介
- 支持增删改查,
- 称之为小型的嵌入式关系型数据库,
- 依赖于系统文件,可以直接获取该数据对象,
- 没有用户概念。
- 在数据库中表中可以不定义字段类型。
- 但是为了程序员之间进行交互,便于数据库的移植,一般在指定字段的时候需要指定该字段的类型。
表的约束
表的约束
- 1.not null 不能为空
- 1.创建表的时候设置
字段名字 字段类型 not null;- 2.创建表的时候设置默认值
字段名字 字段类型 default 默认值 not null;- 3.修改表的约束是不能为空
alter table 表的名字 modify 字段名字 字段的类型 not null;3.修改表的约束是可以为空
alter table 表的名字 modify 字段名字 字段的类型 null;2.unique 不能重复 (null可以重复)
- 1.创建表的设置唯一
字段名字 字段类型 unique;- 2.创建表的设置唯一并且不能为空
字段名字 字段类型 unique not null|(not null unique);- 3.修改表的约束不能重复
alter table 表的名字 modify 字段名字 字段的类型 unique;- 4.修改表的约束不能重复
alter table 表的名字 add unique(字段名);- 5.删除表的约束不能重复(只针对mysql数据库)
alter table 表的名字 drop index 字段名(有约束的字段);6.修改表的约束:多个字段的值合起来是不能重复的。
alter table 表的名字 add constraint 约束名 unique(字段1,..);
7.删除表的约束:多个字段的值合起来是不能重复的。(只针对mysql数据库)
alter table 表的名字 drop index 约束名;3.主键(primary key)
- 1.创建表的时候直接设置主键
字段名字 字段类型 primary key- 2.创建表的时候设置主键,并且自动增长(+1)
字段名 int primary key auto_increment(int 会报错,需要将int 写为integer)
create table user(id integer primary key autoincrement, name varchar(20));- 3.修改表的约束主键
alter table 表的名字 add primary key(字段名);- 4.删除表的主键
alter table 表的名字 drop primary key;
alter table 表的名字 modify 字段名 null;- 5.修改表的约束 多个字段联合做主键
alter table 表的名字 add constraint 约束名 primary key(字段1,..);
表的操作
- 1.创建表
create table 表的名字(字段名字 字段的类型,.. ..);- 2.为所有的字段一一赋值
insert into 表的名字 values(字段值列表);3.表的添加记录
insert into 表的名字(字段列表) values(字段值列表);4.更新一条记录
update 表名 set 字段名=新值,字段名=新值,.. where 条件;- 5.在原来值的基础上做更改
update 表名 set 字段名=字段名(+-*/)+值 where 条件;6.更新某几记录
update 表名 set 字段名=新值… where 主键=值 or 字段=值;
update 表名 set 字段名=新值… where 主键 in(值列表);7.删除一条记录
delete from 表名字 +条件;8.清空所有的记录
delete from 表名字9.查询表中所有的数据
select * from 表的名字;- 10.查询某些字段的数据
select 字段1,字段2… from 表的名字;- 11.where条件查询
select 字段1,字段2… from 表的名字 where 条件1 and 条件2 ….;- 12.增加字段
alter table [表名] add [字段] [类型]
SQLiteDatabase
- db.execSQL(sqlStr) : 执行创建表,增删改 sql语句,查询另有
数据库的更新
在SQLiteOpenHelper中有如下关键方法
- onCreate 初次创建表
- onUpgrade 数据库版本更新进行的操作(修改表的字段,或增加表)
SQLite 数据库三种插入方式
拼接方式
String sql = "insert into Note(title,content,date,tag) values('" + note.title + "','" + note.content + "','" + note.date + "','" + note.tag + "')";
db.execSQL(sql);
使用sql占位符的形式
String sql = "insert into Note(?,?,?,?)";
db.execSQL(sql, new Object[]{note.title, note.content, note.date, note.tag});
使用系统自带的方法
ContentValues contentValues = new ContentValues();
contentValues.put("title",note.title);
contentValues.put("content",note.content);
contentValues.put("date",note.date);
contentValues.put("tag",note.tag);
db.insert("Note",null,contentValues);
SQLite 修改数据
使用拼接方法
String sql = "update Notes set title = '"+note.title+"'" + "where id = '"+note.id+"'";
db.execSQL(sql);
- 如果修改多个字段,用 , 号隔开
使用占位符方法
String sql = "update Notes set title = ? where id = ? ";
db.execSQL(sql,new Object[]{note.title,note.id});
系统默认方法
ContentValues values = new ContentValues();
values.put("title", note.title);
values.put("content", note.content);
values.put("date", note.date);
values.put("tag", note.tag);
db.update("notes", values, "id = ?", new String[]{note.id + ""});
删除数据
使用拼接技术
String sql = "delete from notes where id = '"+id+"'";
db.execSQL(sql);
使用占位符
String sql = "delete from notes where id = ?";
db.execSQL(sql,new Object[]{id});
使用系统方法
db.delete("notes", "id = ? ", new String[]{id + ""});
SQLite使用事务
删除一些数据再插入一些数据,保证两个方法都可以完成
- 开启事务
db.beginTransaction();
…
处理事件
…
db.setTransactionSuccessful();
db.endTransaction();