LitePal open source database framework learning (following the learning of Mr. Guo Lin)

Add dependencies in android studio { compile 'org.litepal.android:core:1.3.2'

}

Add assets folder in app/src/main, add file 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>

Add the Book class

public class Book extends DataSupport{
    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;
    }
}

record classpath test.ban.com.test_litepal.Book

 


Add android:name="org.litepal.LitePalApplication" to the application in Manifast
and complete the configuration, write the main code

 Button createDatabase = (Button) findViewById(R.id.btn_create_table);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Connector.getDatabase();
            }
        });

You can use the creation to
upgrade the database, add forms, etc., just increase the version number <version value="2"></version>, and then modify the fields in the class

 private String press;

    public String getPress() {
        return press;
    }

    public void setPress(String press) {
        this.press = press;
    }

Add table:

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;
    }
}

Just add it in 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>

Inserting data is also easier

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(432);
                book.setPress("aa");
                book.setPrice(321);
                book.save();
            }
        });

Updating data is also relatively simple, just use the method in the DateSupport interface directly

 //更新数据
        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");

            }
        });

But it should be noted that the default value is directly set in this framework. Using the updateAll() method (all data update), it is not possible to use the above method to set, that is, the data cannot be set to the default value, such as The int type is 0, so Litepal provides another setToDefault() method, which can be realized by passing in the column name

Book book=new Book();
book.setToDefault("pages");
book.updateAll();

Deleting data is also relatively simple, just use the deleteAll() method in the flashlight DataSupport, and add a judgment statement

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");
            }
        });

Executing the deleteAll() method directly without specifying constraints will delete the data in the entire table. The
query is the most troublesome. Using Litepal can simplify this part of the content and query all data:

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());
                }

There are some other ways

 //查询第一行
                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);

If you want to use the original sql statement, you can also use
Cursor c=DataSupport.findBySQL("select * from Book where pages > ? and price < ?","400","20");







 

 

Guess you like

Origin blog.csdn.net/u013377003/article/details/54948435