Litepal的使用

来源:互联网 发布:python上使用caffe 编辑:程序博客网 时间:2024/05/16 05:40

Activity

package bwie.litepaldemo;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import org.litepal.LitePal;import org.litepal.crud.DataSupport;import java.util.ArrayList;import java.util.List;/** * 数据库 步骤: * 1.导包:  compile 'org.litepal.android:core:1.4.1' * 2. 配置 litepal.xml:创建assets文件夹,litepal.xml文件:注意是在litepal.xml 建file,建xml就跑下去了 * 3.配置Application * 4.创建数据库:SQLiteDatabase db = LitePal.getDatabase(); * 5.新增一个属性,或新增一张表 之后version 号要加1(如果建两张表,第二张表就直接 extends DataSupport) * 6.进行增删改查的操作 */public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button mAdd;    private Button del_obj;    private Button del_select;    private Button delet_all;    private Button update_obj;    private Button update_select;    private Button update_default;    private Button find_all;    private Button find_first;    private Button find_last;    private Button find_select;    private Button find_where;    private Button find_order;    private Button find_limit;    private Button find_offset;    private Button find_together;    private Button find_native;    private Book b1;    private Book b2;    private Book b3;    private Book b4;    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        //创建数据库        SQLiteDatabase db = LitePal.getDatabase();    }    private void initView() {        tv = (TextView) findViewById(R.id.tv);        mAdd = (Button) findViewById(R.id.add);        del_obj = (Button) findViewById(R.id.del_obj);        del_select = (Button) findViewById(R.id.del_select);        delet_all = (Button) findViewById(R.id.delet_all);        update_obj = (Button) findViewById(R.id.update_obj);        update_select = (Button) findViewById(R.id.update_select);        update_default = (Button) findViewById(R.id.update_default);        find_all = (Button) findViewById(R.id.find_all);        find_first = (Button) findViewById(R.id.find_first);        find_last = (Button) findViewById(R.id.find_last);        find_select = (Button) findViewById(R.id.find_select);        find_where = (Button) findViewById(R.id.find_where);        find_order = (Button) findViewById(R.id.find_order);        find_limit = (Button) findViewById(R.id.find_limit);        find_offset = (Button) findViewById(R.id.find_offset);        find_together = (Button) findViewById(R.id.find_together);        find_native = (Button) findViewById(R.id.find_native);        mAdd.setOnClickListener(this);        del_obj.setOnClickListener(this);        del_select.setOnClickListener(this);        delet_all.setOnClickListener(this);        update_obj.setOnClickListener(this);        update_select.setOnClickListener(this);        update_default.setOnClickListener(this);        find_all.setOnClickListener(this);        find_first.setOnClickListener(this);        find_last.setOnClickListener(this);        find_select.setOnClickListener(this);        find_where.setOnClickListener(this);        find_order.setOnClickListener(this);        find_limit.setOnClickListener(this);        find_offset.setOnClickListener(this);        find_together.setOnClickListener(this);        find_native.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.add:                //添加                b1 = new Book(20, 40f, "吴承恩", "北京出版社", "西游记");                b1.save();                b2 = new Book(80, 50.9f, "曹雪芹", "内蒙古出版社", "红楼梦");                b2.save();                b3 = new Book(60, 66f, "罗贯中", "中国出版社", "三国演义");                b3.save();                b4 = new Book(90, 77.9f, "施耐庵", "内蒙古出版社", "水浒传");                b4.save();                break;            case R.id.del_obj:                b1.delete();                break;            case R.id.del_select:                DataSupport.deleteAll(Book.class, "price>?", "70");                break;            case R.id.delet_all:                DataSupport.deleteAll(Book.class, null);                break;            case R.id.update_obj:                /**                 * 1.对待一存在的对象:重新设置并save();                 *                 */                b1.setPrice(88.8f);                b1.save();                break;            case R.id.update_select:/** * 按条件删除:先new出一个Book实例, * 然后直接调用setPublish(),setPrice()方法来更新数据, * 最后在调用updateAll()去执行更新操作,如果不指定更新条件就表示更新所以数据. */                Book b2 = new Book();                b2.setPublish("上海出版社");                b2.setPrice(100f);                b2.updateAll("name=? and store=?", "水浒传", "90");                break;            /**             *更新置为默认值             */            case R.id.update_default:                Book b1 = new Book();                b1.setToDefault("store");  //字段名                b1.updateAll();                break;/** * 查找的结果是List<Book>集合 */            case R.id.find_all:                List<Book> all = DataSupport.findAll(Book.class);                tv.setText(all.toString());                break;            case R.id.find_first:                Book first = DataSupport.findFirst(Book.class);                tv.setText(first.toString());                break;            case R.id.find_last:                Book last = DataSupport.findLast(Book.class);                tv.setText(last.toString());                break;            case R.id.find_select:                List<Book> books = DataSupport.select("name", "author").find(Book.class);                tv.setText(books.toString());                break;            case R.id.find_where:                List<Book> books1 = DataSupport.where("price<?", "80").find(Book.class);                tv.setText(books1.toString());                break;            case R.id.find_order:                List<Book> books2 = DataSupport.order("store desc").find(Book.class);                tv.setText(books2.toString());                break;            case R.id.find_limit:                List<Book> books3 = DataSupport.limit(2).find(Book.class);                tv.setText(books3.toString());                break;            case R.id.find_offset:                List<Book> books4 = DataSupport.limit(2).offset(2).find(Book.class);                tv.setText(books4.toString());                break;            case R.id.find_together:                List<Book> books5 = DataSupport.select("name", "store", "price")                        .where("store>?", "10")                        .order("store")                        .limit(3)                        .find(Book.class);                tv.setText(books5.toString());                break;            case R.id.find_native:                List<Book> bs = new ArrayList<>();                Cursor cursor = DataSupport.findBySQL("select * from Book where price>? and store>?", "50", "60");                //判空                if (cursor == null) {                    Toast.makeText(this, "没有查询到数据", Toast.LENGTH_SHORT).show();                } else {                    while (cursor.moveToNext()) {                        Book b = new Book();                        b.setName(cursor.getString(cursor.getColumnIndex("name")));                        b.setPrice(cursor.getFloat(cursor.getColumnIndex("price")));                        b.setAuthor(cursor.getString(cursor.getColumnIndex("author")));                        b.setPublish(cursor.getString(cursor.getColumnIndex("publish")));                        b.setStore(cursor.getInt(cursor.getColumnIndex("store")));                        bs.add(b);                    }                }                tv.setText(bs.toString());                break;        }    }}

Application

package bwie.litepaldemo;import android.app.Application;import org.litepal.LitePal;/** * 1.类的用途 * 2.@author:zhaojingjing * 3.2016/12/18. */public class App extends Application {    @Override    public void onCreate() {        super.onCreate();        LitePal.initialize(this);    }}

JavaBean

package bwie.litepaldemo;import org.litepal.crud.DataSupport;/** * 1.类的用途 * 2.@author:zhaojingjing * 3.2016/12/17. */public class Book extends DataSupport {//继承DataSupport    private String name;    private float price;    private String author;    private String publish;    private  int store;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public float getPrice() {        return price;    }    public void setPrice(float price) {        this.price = price;    }    public String getAuthor() {        return author;    }    public void setAuthor(String author) {        this.author = author;    }    public String getPublish() {        return publish;    }    public void setPublish(String publish) {        this.publish = publish;    }    public int getStore() {        return store;    }    public void setStore(int store) {        this.store = store;    }    public Book(int store, float price, String author, String publish, String name) {        this.store = store;        this.price = price;        this.author = author;        this.publish = publish;        this.name = name;    }    public Book() {    }    @Override    public String toString() {        return "Book{" +                "name='" + name + '\'' +                ", price=" + price +                ", author='" + author + '\'' +                ", publish='" + publish + '\'' +                ", store=" + store +                '}'+"\n";    }}
0 0
原创粉丝点击