Open-source Android development framework ------- PowerFramework experiences (4) Database Management DBFarmer and. netframework open-source
DBFarmer is a collection of PowerFramework database management tools.
Objects can be stored. Parameters added with setter and getter are included in the database. Each parameter serves as an item, and the int type id or _ id is used as the primary key.
The database name is DBFarmer. db, which is in the project directory. Replace the full name of the table name of each object with "." with "_". For example, the name of the table com. source. beans. Person is com_source_beans_Person.
private int id = 0;
private int num1 = 0;
private double num2 = 1.0;
private String str = "hello";
private boolean isEnable = true;
public int getNum1 () {
return num1;
}
public void setNum1 (int num1) {
this.num1 = num1;
}
public double getNum2 () {
return num2;
}
public void setNum2 (double num2) {
this.num2 = num2;
}
public String getStr () {
return str;
}
public void setStr (String str) {
this.str = str;
}
public boolean isEnable () {
return isEnable;
}
public void setEnable (boolean isEnable) {
this.isEnable = isEnable;
}
public int getId () {
return id;
}
public void setId (int id) {
this.id = id;
}
1. Insert and call the save method
TestBeanNoID bean = new TestBeanNoID ();
DBFarmer.save (this, bean);
2. Delete the specified id record, deleteById method
DBFarmer.deleteById (this, TestBean.class, 0);
3. Delete all, deleteAll method
DBFarmer.deleteAll (this, TestBean.class, 0);
4. Single condition deletion, deleteByItem method
// Delete the entire content of itemName = itemValue
DBFarmer.deleteByItem (this, TestBean.class, "itemName", "itemValue");
5. Single condition update, updateByItem method
Like deletion, there are currently no support for multiple conditions and like forms, which will be developed in the near future
// Update the entire content of itemName = itemValue
TestBeanNoID bean = new TestBeanNoID ();
DBFarmer.deleteByItem (this, "itemName", "itemValue", bean);
6. Query, all, findAll
List <TestBeanNoID> list = DBFarmer.findAll (this, TestBeanNoID.class);
for (TestBeanNoID b2: list) {
Log.i (TAG, b2.getAccount () + "," + b2.getPassword ());
}
7. Query by ID and find
Find one based on ID. When the JavaBean does not have an id or _id, it cannot be queried and returns null
TestBeanNoID b = DBFarmer.find (this, 0, TestBeanNoID.class);
if (b == null) {
Log.i (TAG, "TestBeanNoID is null");
} else {
Log.i (TAG, "TestBeanNoID id:" + b.getAccount ());
}
8. Empty the database, clearAll
DBFarmer.clearAll (this);
Framework and DEMO file http://www.ideayapai.com/Application/Home/View/default/PowerFamily/index-2.htm
Welcome everyone Daniel and Great God to visit the QQ group for communication: 575026537