LitePal是一款开源的Android数据库框架,采用了对象关系映射(ORM)的模式,将平时开发时最常用的一些数据库功能进行了封装,使得开发者不用编写一行SQL语句就可以完成各种建表、増删改查的操作。
编辑build.gradle文件并添加下面的依赖关系
dependencies {
compile ‘org.litepal.android:core:1.6.1’
}
右击app/src/main目录,创建一个assets文件夹,然后在这个文件夹下创建一个litepal.xml的文件,接着编辑这个文件中的内容,如下面所示:
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
list>
litepal>
"org.litepal.LitePalApplication"
...
>
...
public class Album extends DataSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
private float price;
private byte[] cover;
private List songs = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public byte[] getCover() {
return cover;
}
public void setCover(byte[] cover) {
this.cover = cover;
}
public List getSongs() {
return songs;
}
public void setSongs(List songs) {
this.songs = songs;
}
}
public class Song extends DataSupport {
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)
private String uselessField;
private Album album;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getUselessField() {
return uselessField;
}
public void setUselessField(String uselessField) {
this.uselessField = uselessField;
}
public Album getAlbum() {
return album;
}
public void setAlbum(Album album) {
this.album = album;
}
}
<list>
<mapping class="com.example.litepal.Album" />
<mapping class="com.example.litepal.Song" />
list>
这些表格会在你操作数据库时自动生成
SQLiteDatabase db = LitePal.getDatabase();
注意点:
String albumName = albumEt.getText().toString();
float price = 109.9f;
Album album = new Album();
album.setName(albumName);
album.setPrice(price);
album.save();//保存至数据库
Album album1= DataSupport.find(Album.class,1);
album1.setName("Fantasy");//设置修改后的名称
album1.save();
//int row=DataSupport.delete(Album.class,2);
int row=DataSupport.deleteAll(Album.class,"id>?","2");//第一个参数用于指定删除哪个表中的数据,后面的为约束条件
Toast.makeText(this,"您删除了"+row+"行数据",Toast.LENGTH_SHORT).show();
List albumList = DataSupport.findAll(Album.class);//查询表中所有数据
//List albumList=DataSupport.where("id and name like ?","6","a%").order("name").limit(5).find(Album.class);
for (Album a:
albumList)//写个for循环
Log.e("MAIN ",a.getName()+"***");
布局文件中
id="@+id/main_album_et"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="专辑名" />
Activity中
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText albumEt;
private Button addAlbumBtn;
private Button modifyAlbumBtn;
private Button deleteAlbumBtn;
private Button queryAlbumBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SQLiteDatabase db = LitePal.getDatabase();
bindId();
}
private void bindId() {
albumEt = findViewById(R.id.main_album_et);
addAlbumBtn = findViewById(R.id.main_add_album_btn);
modifyAlbumBtn = findViewById(R.id.main_modify_album_btn);
deleteAlbumBtn = findViewById(R.id.main_delete_album_btn);
queryAlbumBtn=findViewById(R.id.main_query_album_btn);
addAlbumBtn.setOnClickListener(this);
modifyAlbumBtn.setOnClickListener(this);
deleteAlbumBtn.setOnClickListener(this);
queryAlbumBtn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.main_add_album_btn:
String albumName = albumEt.getText().toString();
float price = 109.9f;
Album album = new Album();
album.setName(albumName);
album.setPrice(price);
album.save();//保存至数据库
DebugDB.getAddressLog();
break;
case R.id.main_modify_album_btn:
Album album1= DataSupport.find(Album.class,1);
album1.setName("Fantasy");
album1.save();
break;
case R.id.main_delete_album_btn:
//int row=DataSupport.delete(Album.class,2);
int row=DataSupport.deleteAll(Album.class,"id>?","2");
Toast.makeText(this,"您删除了"+row+"行数据",Toast.LENGTH_SHORT).show();
break;
case R.id.main_query_album_btn:
List albumList = DataSupport.findAll(Album.class);
//List albumList=DataSupport.where("id and name like ?","6","a%").order("name").limit(5).find(Album.class);
for (Album a:
albumList)
Log.e("MAIN ",a.getName()+"***");
break;
}
}
}