安卓图书馆,使开发人员可以使用SQLite数据库极为容易。使用方便
使用AndroidStudio
compile 'org.litepal.android:core:1.6.1'
debugImplementation 'com.amitshekhar.android:debug-db:1.0.3'
配置litepal.xml
在Project模式下,main根目录下创建文件夹assets,并在此文件夹中创建file,命名为litepal.xml
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
<mapping class="com.homework.activity.litepaldemo.Album"/>
<mapping class="com.homework.activity.litepaldemo.Song"/>
list>
litepal>
配置litepalapplication
在AndroidManifest中添加
"org.litepal.LitePalApplication"
...
>
...
public class Album extends DataSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
private float price;
private List songs = new ArrayList<>();
// generated getters and setters.添加构造方法
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 List getBooks() {
return songs;
}
public void setBooks(List books) {
this.songs = books;
}
}
public class Song extends DataSupport{
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)
private String uselessField;
private Album album;
// generated getters and setters.添加构造方法
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.homework.activity.litepaldemo.Album"/>
<mapping class="com.homework.activity.litepaldemo.Song"/>
list>
保存数据
简单的创建数据库,在Mainactivity添加一行代码
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);//id为1的数据
album1.setName("Fantasy");//要修改成的代码
album1.save();
删
//直接删除id为2的数据
int row=DataSupport.delete(Album.class,2);
//删除多行数据
int row=DataSupport.deleteAll(Album.class,"id>?","3");
Toast.makeText(this,"您删除了"+row+"行数据",Toast.LENGTH_SHORT).show();
查
//显示全部
List albumList=DataSupport.findAll(Album.class);
//按照要求筛选显示
List albumList=DataSupport.where("id>? and name like ?","9","a%").find(Album.class);
//按照name从小到大排列显示
List albumList=DataSupport.where("name like ?","a%").order("name").find(Album.class);
在后面添加foreach循环,打印日志
for (Album a:
albumList) {
Log.e("MAIN",a.getName()+"****");
}
Mainactivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText albumEt;
private Button addalbumBtn;
private Button modifyalbumBtn;
private Button deletealbumBtn;
private Button queytalbumBtn;
@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.album_et);
addalbumBtn=findViewById(R.id.add_album_btn);
modifyalbumBtn=findViewById(R.id.modify_album_btn);
deletealbumBtn=findViewById(R.id.delete_album_btn);
queytalbumBtn=findViewById(R.id.query_album_btn);
addalbumBtn.setOnClickListener(this);
modifyalbumBtn.setOnClickListener(this);
deletealbumBtn.setOnClickListener(this);
queytalbumBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add_album_btn:
String albumName=albumEt.getText().toString();
float price=109.9f;
Album album=new Album();
album.setName(albumName);
album.setPrice(price);
album.save();//保存至数据库
break;
case R.id.modify_album_btn:
Album album1= DataSupport.find(Album.class,1);
album1.setName("Fantasy");
album1.save();
break;
case R.id.delete_album_btn:
// int row=DataSupport.delete(Album.class,2);
int row=DataSupport.deleteAll(Album.class,"id>?","3");
Toast.makeText(this,"您删除了"+row+"行数据",Toast.LENGTH_SHORT).show();
break;
case R.id.query_album_btn:
// List albumList=DataSupport.findAll(Album.class);//显示全部
// List albumList=DataSupport.where("id>? and name like ?","9","a%").find(Album.class);//按照要求筛选
List albumList=DataSupport.where("name like ?","a%").order("name").find(Album.class);//按照name从小到大排列
for (Album a:
albumList) {
Log.e("MAIN",a.getName()+"****");
}
break;
}
}
}
litepal.xml
<litepal>
<dbname value="demo" />
<version value="1" />
<list>
<mapping class="com.homework.activity.litepaldemo.Album"/>
<mapping class="com.homework.activity.litepaldemo.Song"/>
list>
litepal>
activity_main
"http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.homework.activity.litepaldemo.MainActivity">
id="@+id/album_et"
android:layout_width="match_parent"
android:layout_height="50dp" />