首页 > 学院 > 开发设计 > 正文

LitePal 开源数据库框架学习(跟随郭霖老师的学习)

2019-11-09 15:51:17
字体:
来源:转载
供稿:网友
在android studio中添加dependdencies{compile‘org.litepal.android:core:1.3.2’

}

在app/src/main中添加assets文件夹,添加文件litepal.xml

<?xml version="1.0" encoding="utf-8"?><litepal>    <dbname value="BookStore"></dbname>    <version value="1"></version>    <list>        <mapping class="test.ban.com.test_litepal.Book"></mapping>    </list></litepal>添加Book类
public class Book {    PRivate int id;    private String author;    private double price;    private int pages;    private String name;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    public int getPages() {        return pages;    }    public void setPages(int pages) {        this.pages = pages;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}记录类路径test.ban.com.test_litepal.Book

在Manifast中的application添加android:name="org.litepal.LitePalApplication"配置完成,编写主代码

 Button createDatabase = (Button) findViewById(R.id.btn_create_table);        createDatabase.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Connector.getDatabase();            }        });即可使用创建完成升级数据库,添加表单等,只需要增加版本号<version value="2"></version>,然后修改类中的字段即可
 private String press;    public String getPress() {        return press;    }    public void setPress(String press) {        this.press = press;    }添加表:
public class Category {    private int id;    private String categoryName;    private int categoryCode;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getCategoryName() {        return categoryName;    }    public void setCategoryName(String categoryName) {        this.categoryName = categoryName;    }    public int getCategoryCode() {        return categoryCode;    }    public void setCategoryCode(int categoryCode) {        this.categoryCode = categoryCode;    }}在litepal中添加即可:
<?xml version="1.0" encoding="utf-8"?><litepal>    <dbname value="BookStore"></dbname>    <version value="3"></version>    <list>        <mapping class="test.ban.com.test_litepal.Book"></mapping>        <mapping class="test.ban.com.test_litepal.Category"></mapping>    </list></litepal>插入数据也较为简单
Button addDate= (Button) findViewById(R.id.btn_insert_table);        addDate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Book book=new Book();                book.setName("壁纸碧水");                book.setAuthor("ban");                book.setPages(520);                book.setPress("yy");                book.setPrice(1314);                book.save();            }        });更新数据也较为简单,直接使用DateSupport接口中的方法即可
 //更新数据        Button updateDate= (Button) findViewById(R.id.btn_update_table);        updateDate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Book book=new Book();//                book.setName("小河在流血");//                book.setAuthor("ban");//                book.setPages(123);//                book.setPress("标签");//                book.setPrice(50);//                book.save();//                book.setPrice(20);//                book.save();                book.setPrice(15.66);                book.setPress("快乐时");                book.updateAll("name=? and author=?","小河在流血","ban");            }        });但需要注意的是,此框架中是直接设置了默认值的,使用updateAll()方法(所有的数据更新),是不可以使用上面的方式来set的,就是不能讲数据设置为默认值,比如int类型的为0,所以Litepal提供了另一个setToDefault()方法,传入列名就可以实现了
Book book=new Book();book.setToDefault("pages");book.updateAll();删除数据也较为简单,直接电筒DataSupport中的deleteAll()方法即可,再加上判断语句
Button deleteDate = (Button) findViewById(R.id.btn_delete_table);        deleteDate.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                DataSupport.deleteAll(Book.class, "price<?", "16");            }        });直接执行deleteAll()方法不指定约束条件,则是将整个表的数据删除查询,都是最麻烦的,使用Litepal就可以简化这一部分的内容查询所有数据:
List<Book> books = DataSupport.findAll(Book.class);                for (Book book : books) {                    Log.i(TAG, "name: " + book.getName());                    Log.i(TAG, "price: " + book.getPrice());                    Log.i(TAG, "author: " + book.getAuthor());                    Log.i(TAG, "press: " + book.getPress());                    Log.i(TAG, "pages: " + book.getPages());                }还有一些其他的方法
 //查询第一行                Book firstBook = DataSupport.findFirst(Book.class);                //查询最后一行                Book lastBook = DataSupport.findLast(Book.class);                List<Book> books = DataSupport.select("name", "author").find(Book.class);//                for (Book book : books) {//没有查询出的值为空(默认值)//                    Log.i(TAG, "name: " + book.getName());//                    Log.i(TAG, "price: " + book.getPrice());//                    Log.i(TAG, "author: " + book.getAuthor());//                    Log.i(TAG, "press: " + book.getPress());//                    Log.i(TAG, "pages: " + book.getPages());//                }                //倒序排列                List<Book> books2 = DataSupport.order("price desc").find(Book.class);                //限制                List<Book> books3 = DataSupport.limit(3).offset(1).find(Book.class);                //where 子句                List<Book> books4 = DataSupport.where("pages>?", "10").find(Book.class);若是想用原省的sql语句,也可以使用Cursor c=DataSupport.findBySQL("select * from Book where pages > ? and price < ?","400","20");


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表