litepal开源数据库工具

首先添加如下依赖

compile 'org.litepal.android:core:1.6.0'

新建assets文件夹,然后在文件夹中添加litepal.xml文件,如下图所示


litepal开源数据库工具_第1张图片
s.png

litepal.xml文件中,内容如下:



    数据库名称
    数据库版本
    
        
    

修改androidmanifest中的application的name属性。如下所示:

Book文件中

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

Mainactivity文件中

public class MainActivity extends AppCompatActivity {
    private Button button;
    private Button button1;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Connector.getDatabase();
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button1);
        button2= (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setName("老人与海");
                book.setAuthor("海明威");
                book.setPages(444);
                book.setPrice(20.0);
                book.save();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book=new Book();
                book.setPrice(30.0);
                book.updateAll("name='老人与海'");
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class,"name='老人与海'");
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Listlist=DataSupport.findAll(Book.class);
                for(Book book:list){
                    Log.i("xinxi",book.getName()+","+book.getAuthor()+","+book.getPages()+","+book.getPrice());
                }
            }
        });
    }
}



    
litepal开源数据库工具_第2张图片
device-2017-11-12-202612.png

你可能感兴趣的:(litepal开源数据库工具)