- 一、依赖引入
- 二、 *** 作步骤
- 1.创建实例
- 2.使用application启动
- 3.使用litepale
一、依赖引入
二、 *** 作步骤 1.创建实例implementation ‘org.litepal.guolindev:core:3.2.3’
代码如下(示例):
需要extends LitePalSupport
public class Category extends LitePalSupport { private int id; private String categoryName; private int categoryCode; public void setId(int id) { this.id = id; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } public void setCategoryCode(int categoryCode) { this.categoryCode = categoryCode; } public int getId() { return id; } public String getCategoryName() { return categoryName; } public int getCategoryCode() { return categoryCode; } }2.使用application启动
代码如下(示例):
记得注册application
public class MyOwnApplication extends Application { @Override public void onCreate() { super.onCreate(); LitePal.initialize(this); } }3.使用litepale
public class MainActivity extends AppCompatActivity { private static String TAG="MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button createDatabase = findViewById(R.id.create_database); createDatabase.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Book book = new Book(); book.setName("wjc"); book.setAuthor("wjc"); book.setPages(252); book.setPrice(16.1); book.setPress("unknow"); //插入数据 // book.save(); // book.setPrice(20); //更该数据 // Book book1 = LitePal.find(Book.class, 12); // book1.setPrice(20.99f); // raise the price // book1.save(); //更新数据 // Book book1 = new Book(); // book1.setPrice(20.11f); // raise the price // book.update(1); //更新通过条件 // Book book1 = new Book(); // book.setPrice(20.22f); // raise the price // book.updateAll("id= ?", "12"); //按id除去表 // LitePal.delete(Book.class, 1); //按指定条件除去符合条件所有的 // LitePal.deleteAll(Book.class,"price>?","30"); //Query data // Book book1=LitePal.find(Book.class,12); // Log.d(TAG,book1+""); //查找全部 // ListallBooks = LitePal.findAll(Book.class); // for (Book book1 :allBooks){ // Log.d(TAG,book1+""); // } //通过条件查询 List books = LitePal. where("author like ? and price < ?", "wjc%", "200") .order("id").find(Book.class); for (Book book1 :books){ Log.d(TAG,book1+""); } } }); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)