1.创建数据库
在build.gradle中添加
compile 'org.litepal.android:core:1.6.1'
创建一个assets文件夹,然后在assets中添加一个litepal.xml配置文件。dbname为数据库的名称,version为版本号,list中添加的是表对应的javabean
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value="BookStore"></dbname>
<version value="3"></version>
<list>
<mapping class="com.firstlinecode.datasave.litepal.Book"/>
</list>
</litepal>
创建Javabean,并且继承DataSupport
public class Book extends DataSupport {
private int id;
private String name;
private String author;
private int pages;
private double price;
private String press;
...
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", pages=" + pages +
", price=" + price +
", press='" + press + '\'' +
'}';
}
修改AndroidManifest.xml的application,将name改为如下的名称
<application
android:name="org.litepal.LitePalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
2.升级数据库
只需要修改litepal.xml配置文件的版本号;修改和添加字段只需要在javabean中修改就可以了;添加表需要在litepal.xml配置文件的list中添加内容,再添加Javabean类。
3.数据表的增删改查
public class LitePalActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lite_pal);
findViewById(R.id.litePal_create).setOnClickListener(this);
findViewById(R.id.litePal_insert).setOnClickListener(this);
findViewById(R.id.litePal_update).setOnClickListener(this);
findViewById(R.id.litePal_delete).setOnClickListener(this);
findViewById(R.id.litePal_query).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.litePal_create:
Connector.getDatabase();
Toast.makeText(LitePalActivity.this,"创建了数据库",Toast.LENGTH_SHORT).show();
break;
case R.id.litePal_insert:
Book book=new Book();
book.setName("第一行代码");
book.setAuthor("郭林");
book.setPages(800);
book.setPrice(29.0);
book.setPress("kkwee");
book.save();
Toast.makeText(LitePalActivity.this,"插入数据",Toast.LENGTH_SHORT).show();
break;
case R.id.litePal_update:
Book updateBook=new Book();
updateBook.setPrice(55.0);
updateBook.setPress("kk");
updateBook.updateAll("name=?","第一行代码");
Toast.makeText(LitePalActivity.this,"修改数据",Toast.LENGTH_SHORT).show();
break;
case R.id.litePal_delete:
DataSupport.deleteAll(Book.class,"price>?","30");
Toast.makeText(LitePalActivity.this,"删除数据",Toast.LENGTH_SHORT).show();
break;
case R.id.litePal_query:
List<Book>bookList= DataSupport.findAll(Book.class);//表示select* from book
// List<Book>bookList= DataSupport.select("name","author","pages","price").where("pages>?","700").order("pages").limit(10).offset(1).find(Book.class);//查找这四列pages大于300,按照pages排列,查找前10行,从第二行开始排列
for (Book book1:bookList){
Log.d("csc",book1.toString());
}
Toast.makeText(LitePalActivity.this,"数据查询成功",Toast.LENGTH_SHORT).show();
break;
}
}
}
网友评论