compile 'org.litepal.android:core:1.6.0'
<application
android:name="org.litepal.LitePalApplication"
如果有自己的application,那么继承于LitePalApplication
并在自定义的Application中的oncreate()中 LitePal.initialize(this)
继承DataSupport
public class Model extends DataSupport {
//必须为private否则不能被添加为数据库
private String name;
private Byte img;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getImg() {
return img;
}
public void setImg(Byte img) {
this.img = img;
}
}
创建assets目录
在as中将视图切换为project模式,右键main,new folder
<litepal>
<dbname value="db" >dbname>
<version value="1" >version>
<list>
<mapping class="com.junx.litepaldatabase.Model">mapping>
<mapping class="com.junx.litepaldatabase.User">mapping>
list>
litepal>
但发现litepal不支持byte[]数组
//bitmap转byte[]
public byte[] img(int id) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(id)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
//byte[]转bitmap
public byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
public static String bitmapToBase64(Bitmap bitmap) {
String result = null;
ByteArrayOutputStream baos = null;
try {
if (bitmap != null) {
baos = new ByteArrayOutputStream();
//如果有透明的部分,解码后该背景会变黑
//有需要就将格式改为png
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
baos.flush();
baos.close();
byte[] bitmapBytes = baos.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.flush();
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static Bitmap base64ToBitmap(String base64Data) {
byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length );
}
public class hehe extends DataSupport {
private String name;
private byte[] bytes;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}
添加到映射表中
<litepal>
<dbname value="db" >dbname>
<version value="1" >version>
<list>
<mapping class="com.junx.litepaldatabase.hehe">mapping>
list>
litepal>
//添加新的字段或者新建一个表
public class hehe extends DataSupport {
private String name;
private byte[] bytes;
//添加新的字段
private String updateString;
public String getUpdateString() {
return updateString;
}
public void setUpdateString(String updateString) {
this.updateString = updateString;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}
将映射表中的版本号+1
<litepal>
<dbname value="db" >dbname>
<version value="2" >version> //原本为2
<list>
<mapping class="com.junx.litepaldatabase.hehe">mapping>
list>
litepal>
//为了实现数据的CURD操作,必须Model继承DataSupport
//只是创建表的话可以不继承,在映射表中添加映射即可
public class hehe extends DataSupport {
private String name;
private byte[] bytes;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}
new hehe().save();//保存到数据库
//获取到旧数据以后修改,执行save()方法即可保存
Model model = DataSupport.findLast(Model.class);
model.setName("newName");
model.save();
//批量修改
hehe last = DataSupport.findLast(hehe.class);
last.setName("newName");
//约束参数为约束条件,=的作用为equal,不传入参数就是全部更新
last.updateAll("name!=?","newName");
//删除指定数据
hehe last = DataSupport.findLast(hehe.class);
last.delete();
//删除指定类型的全部数据,返回的是删除成功的数量
int i = DataSupport.deleteAll(hehe.class);
//删除指定约束条件的
for (int i = 0; i <50 ; i++) {
hehe hehe = new hehe();
hehe.setName(i%2==0?"should be deleted":"should not be deleted");
hehe.save();
}
//i为25
int i = DataSupport.deleteAll(hehe.class, "name=?", "should be deleted");
//i为50
int i = DataSupport.deleteAll(hehe.class, "name=? or name=?","should be deleted","should not be deleted");
//查询指定类型的全部数据
List all = DataSupport.findAll(hehe.class);
//也可以指定id
List all = DataSupport.findAll(hehe.class, 0, 1);
//查询表中的第一条数据
List all = DataSupport.findAll(hehe.class, 0, 1);
//查询指定表的第一个数据
hehe first = DataSupport.findFirst(hehe.class);
//查询指定表的最后一个数据
hehe last = DataSupport.findLast(hehe.class);
//下面的查询方式可以链式调用
//查询指定约束条件的数据,对应sql中的where
List lists = DataSupport.where("name!=?", "null").find(hehe.class);
//指定结果的排序方式,对应sql中的order by
DataSupport.order("price desc").find(hehe.class);
//指定查询结果的数量
DataSupport.limit(3).find(hehe.class);
//指定查询结果的偏移量,例如查询低2 3 4条数据时
DataSupport.limit(3).offset(1).find(hehe.class);
//原生的查询方式
Cursor c = DataSupport.findBySQL("select * from hehe");
if (c!=null){
Log.v("meee",getClass()+":\n"+"");
while(c.moveToNext()){
int name = c.getColumnIndex("name");
String nameString = c.getString(name);
}
c.close();
}
Cursor 是每行的集合。
使用 moveToFirst() 定位第一行。
你必须知道每一列的名称。
你必须知道每一列的数据类型。
Cursor 是一个随机的数据源。
if (c!=null){
Log.v("meee",getClass()+":\n"+"");
while(c.moveToNext()){
int name = c.getColumnIndex("name");
String nameString = c.getString(name);
}
}
cursor的使用方法
private void CursorApi(Cursor cursor){
//关闭游标,释放资源
cursor.close();
//在缓冲区中检索请求的列的文本,将将其存储
// cursor.copyStringToBuffer(int,chararraybuffer);
//返回所有列的总数
cursor.getColumnCount();
//返回指定列的名称,如果不存在返回-1
cursor.getColumnIndex("columnName");
//从0开始返回指定列的名称,如果不存在抛出IllegalArgumentException
cursor.getColumnIndexOrThrow("columnName");
//从给定的索引返回列名
cursor.getColumnName(0);
//返回列名的String[]
cursor.getColumnNames();
//返回行数
cursor.getCount();
//移动光标到第一行
cursor.moveToFirst();
//移动光标到最后一行
cursor.moveToLast();
//移动光标到下一行
cursor.moveToNext();
//移动光标到指定行数
cursor.moveToPosition(0);
//移动光标到上一行
cursor.moveToPrevious();
}