litepal更好的操作sqlite3,配置与基本操作

litepal更好的操作sqlite3

配置

在app下的build.gradle中加入

litepal更好的操作sqlite3,配置与基本操作

创建litepal.xml配置

在app/src/main下面创建assets,新建litepal.xml

<?xml version="1.0" encoding="utf-8" ?>
    <litepal>
        <dbname value="BookStore"></dbname>
        <version value="2"></version>

        <list>
            <!-- 映射到类 -->
            <mapping class="demo.jq.com.litepaltest.Book"></mapping>
            <mapping class="demo.jq.com.litepaltest.Category"></mapping>
        </list>
    </litepal>

配置LitePalApplication,在AndroidMainfest.xml中

litepal更好的操作sqlite3,配置与基本操作

创建Book类

package demo.jq.com.litepaltest;

/**
 * @author Jim
 */

public class Book {
    private int id;
    private String author;
    private double price;
    /**
     * 出版社
     */
    private String press;
    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;
    }

    public String getPress() {
        return press;
    }

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

创建分类类

package demo.jq.com.litepaltest;

/**
 * @author jim
 */

public class Category {
    private int id;
    private String categoryName;
    private int categoryCode;
    public void setId(int id) {
        this.id = id;
    }
    public int getId() {
        return id;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryCode(int categoryCode) {
        this.categoryCode = categoryCode;
    }
    public int getCategoryCode() {
        return categoryCode;
    }
}

这个时候,调用Connector.getDatabase();

就会自动创建数据库。

import org.litepal.tablemanager.Connector;

...

// 初始化创建按钮
        Button createDatabase = (Button) findViewById(R.id.create_database);
        // 设置点击事件
        createDatabase.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // 执行写入数据库操作
                Connector.getDatabase();
            }
        });

添加数据

// 初始化添加数据按钮
        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setName("The Da Vinci Code");
                book.setAuthor("Dan Brown");
                book.setPages(454);
                book.setPrice(16.96);
                book.setPress("XinNet");
                book.save();
            }
        });

更改数据

// 更改数据
        Button updateData = (Button) findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setPress("XinHua.Net");
                book.update(1);
            }
        });

删除数据

// 删除数据
        Button deleteData = (Button) findViewById(R.id.delete_data);
        deleteData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class,"id = ?","2");
            }
        });

查询数据

// 查询数据
        Button queryData = (Button) findViewById(R.id.query_data);
        queryData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                List<Book> books = DataSupport.findAll(Book.class);
                for (Book book :books) {
                    Log.d(TAG,"book name is "+book.getName());
                    Log.d(TAG,"book author is "+book.getAuthor());
                    Log.d(TAG,"book pages is "+book.getAuthor());
                    Log.d(TAG,"book price is "+book.getPrice());
                    Log.d(TAG,"book press is "+book.getPress());
                    Log.d(TAG,"--------------------------");
                }
            }
        });

litepal更好的操作sqlite3,配置与基本操作

更多查询

// 查询数据
        Button queryData = (Button) findViewById(R.id.query_data);
        queryData.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                Book firstBook = DataSupport.findFirst(Book.class);
                Log.d(TAG,"the id is "+firstBook.getId());
                Log.d(TAG,"book name is "+firstBook.getName());
                Log.d(TAG,"book author is "+firstBook.getAuthor());
                Log.d(TAG,"book pages is "+firstBook.getAuthor());
                Log.d(TAG,"book price is "+firstBook.getPrice());
                Log.d(TAG,"book press is "+firstBook.getPress());
                Log.d(TAG,"--------------------------");

                Book lastBook = DataSupport.findLast(Book.class);
                Log.d(TAG,"the id is "+lastBook.getId());
                Log.d(TAG,"book name is "+lastBook.getName());
                Log.d(TAG,"book author is "+lastBook.getAuthor());
                Log.d(TAG,"book pages is "+lastBook.getAuthor());
                Log.d(TAG,"book price is "+lastBook.getPrice());
                Log.d(TAG,"book press is "+lastBook.getPress());
                Log.d(TAG,"--------------------------");
            }
        });


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7700277.html,如需转载请自行联系原作者
上一篇:Spring Cloud Alibaba | Sentinel: 服务限流基础篇


下一篇:存储引擎之必知必会 -- 恢复间隔(分钟)Recovery Interval