假设我们想要对Book表进行CUDR操作,首先需要在Book类中添加Book类的继承结构:
1 public class Book extends DataSupport{ 2 ... 3 }
这是因为要对表内数据进行操作需要继承DataSupport类。
1.添加数据
1 //添加数据到Book表中 2 Button addData = findViewById(R.id.add_data); 3 addData.setOnClickListener(new View.OnClickListener() { 4 @Override 5 public void onClick(View v) { 6 Book book = new Book(); 7 book.setName("Thinking In Java (Java编程思想)"); 8 book.setAuthor("Bruce Eckel"); 9 book.setPages(880); 10 book.setPrice(79.82); 11 book.save(); 12 Toast.makeText(MainActivity.this,"第一条数据添加成功",Toast.LENGTH_LONG).show(); 13 Book book1 = new Book(); 14 book1.setName("Android应用"); 15 book1.setAuthor("BruceLi"); 16 book1.setPages(650); 17 book1.setPrice(35.6); 18 book1.save(); 19 Toast.makeText(MainActivity.this,"第二条数据添加成功",Toast.LENGTH_LONG).show(); 20 } 21 });
2.更新数据
1 //更新数据 2 Button updateData = findViewById(R.id.update_data); 3 updateData.setOnClickListener(new View.OnClickListener() { 4 @Override 5 public void onClick(View v) { 6 Book bookUpdate = new Book(); 7 bookUpdate.setPrice(44.9); 8 bookUpdate.update(2); 9 Toast.makeText(MainActivity.this,"价格更新成功",Toast.LENGTH_LONG).show(); 10 } 11 });