【Java转Android】30. LitePal操作SQLite数据库

30. LitePal操作SQLite数据库

LitePal:https://github.com/LitePalFramework/LitePal
在这里插入图片描述


<litepal>
    <dbname value="BookShop"/>
    <version value="2"/>
    <list>
        <mapping class="nopi.aystudio.mthread.Book"/>
        <mapping class="nopi.aystudio.mthread.Category"/>
    list>
litepal>

package nopi.aystudio.mthread;

import org.litepal.crud.LitePalSupport;

public class Book extends LitePalSupport {

    private int id;
    private String author;
    private double price;
    private int pages;
    private String name;
    private String press;

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

    public String getPress() {
        return press;
    }

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

package nopi.aystudio.mthread;

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

   case R.id.btn_get1:
                Connector.getDatabase();
                Toast.makeText(this, "创建成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_add1:
                Book book = new Book();
                book.setName("Tom Menu");
                book.setAuthor("Tom");
                book.setPages(454);
                book.setPrice(16.96);
                book.setPress("Unknow");
                book.save();

                Book book1 = new Book();
                book1.setName("Alice Menu");
                book1.setAuthor("Alice");
                book1.setPages(531);
                book1.setPrice(61.96);
                book1.setPress("Unknow");
                book1.save();
                break;
            case R.id.btn_del1:
                LitePal.deleteAll(Book.class,"price < ?","30");
                break;

            case R.id.btn_read1:
                List<Book> books = LitePal.findAll(Book.class);
                for(Book book3 :books){
                    Log.d(TAG, "onClick: "+book3.getName());
                    Log.d(TAG, "onClick: "+book3.getAuthor());
                    Log.d(TAG, "onClick: "+book3.getPress());
                    Log.d(TAG, "onClick: "+book3.getPages());
                    Log.d(TAG, "onClick: "+book3.getPrice());
                }
                break;
            case R.id.btn_update1:
                Book book2 = new Book();
                book2.setPrice(66.66);
                book2.setPress("出版社");
                book2.updateAll("author = ?","Alice");
                break;

【Java转Android】30. LitePal操作SQLite数据库_第1张图片

  android:name="org.litepal.LitePalApplication"

你可能感兴趣的:(Java转Android)