学了Android近三个月,终于完成了需求,做了一款App,客户反应还比较满意,对Android进行一些总结,以后还有很多地方需要学习
以下的东西都是我自己的理解.
首先,LitePal是一个操纵SQLite的数据库框架,思想跟hibernate,mybatis类似.它的优点就是轻量化,使用起来也比较方便.有JavaWeb的基础的话,理解起来还是很简单的.
1.引用gradle依赖(也可以引入Jar包)
dependencies {
compile 'org.litepal.android:core:1.5.0'
}
2.配置litepal.xml
litepal.xml 在app/assets/ 目录下
<?xml version="1.0" encoding="utf-8"?> <litepal> <!-- Define the database name of your application. By default each database name should be end with .db. If you didn't name your database end with .db, LitePal would plus the suffix automatically for you. For example: <dbname value="demo" /> --> <dbname value="数据库名" /> <!-- Define the version of your database. Each time you want to upgrade your database, the version tag would helps. Modify the models you defined in the mapping tag, and just make the version value plus one, the upgrade of database will be processed automatically without concern. For example: <version value="1" /> --> <version value="1" /> <!-- Define your models in the list with mapping tag, LitePal will create tables for each mapping class. The supported fields defined in models will be mapped into columns. For example: <list> <mapping class="com.test.model.Reader" /> <mapping class="com.test.model.Magazine" /> </list> --> <list> <mapping class="实体类的路径"></mapping> </list> <!-- Define where the .db file should be. "internal" means the .db file will be stored in the database folder of internal storage which no one can access. "external" means the .db file will be stored in the path to the directory on the primary external storage device where the application can place persistent files it owns which everyone can access. "internal" will act as default. For example: <storage value="external" /> --> </litepal>
3.创建实体类继承DataSupport
public class Article extends DataSupport { private int id; private String headline; private String author; private String content; private String time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getHeadline() { return headline; } public void setHeadline(String headline) { this.headline = headline; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }
4.查
//查询id为3的Article Article article = DataSupport.find(Article.class, 3); //查询第一条Article Article article1 = DataSupport.findFirst(Article.class); //查询最后一条Article Article article2 = DataSupport.findLast(Article.class); //查询所有的 List<Article> articleList = DataSupport.findAll(Article.class); //查询特定的 List<Article> articleList1 = DataSupport.where("id > ?", "1").find(Article.class); //统计表的行数,Count int number = DataSupport.count(Article.class);
5.增
Article article1= new Article(); article1.setId(article.getId()); article1.setAuthor(article.getAuthor()); article1.setHeadline(article.getHeadline()); article1.setContent(article.getContent()); article1.setTime(article.getTime()); article1.save();
6.删
DataSupport.deleteAll(Article.class, "id=?", "1");
删除id为1的article
7.改
Article article1= new Article(); article1.setId(article.getId()); article1.setAuthor(article.getAuthor()); article1.setHeadline(article.getHeadline()); article1.setContent(article.getContent()); article1.setTime(article.getTime()); article1.update(1);更新了id为1的Article