Db.delete (student);
Android database does not actually use much, the network cache in the Dhroid framework uses a database I wrote a database manipulation tool
Dhroid database Basic or Single table operation more, for the sake of simplicity I only made a single table, those cascade, lazy load, what the two operating areas to solve it
Initialize the IOC-based configuration in application
// Database initialization
DhDB db = IocContainer.getShare (). Get (DhDB.class);
db.init ("dhdbname", Const.DATABASE_VERSION);
If your database is on the sd card, you can
db.initInSD ("Folder location", "dhdbname", Const.DATABASE_VERSION);
bean definition
@Entity (table = "student")
public class Student {
@Column (pk = true) // The primary key cannot be a basic type
public Long id;
public String name;
@Column (name = "num_no")
public String num;
@Column (name = "create_time")
public Date createTime;
public int age;
public int sex;
public boolean dangyuang;
@NoColumn
public String temp;
// setget ...
}
@Entity (table = "student") indicates that this object is persistent, you can not add it, the default is OK, the default object name is the same as the table name
The default attributes will be persisted with the same default column name and attribute name
@Column (pk = true) defines the primary key
@Column (name = "num_no") defines the column name
@NoColumn means not to participate in persistence
Properties support basic data types
Get the Dhdb object. Below is the ioc I used. You can also make a singleton
@Inject
DhDB db;
Save and update
if (student == null) {
student = new Student ();
isnew = true;
}
student.setName (nameV.getText (). toString ());
student.setNum (numV.getText (). toString ());
student.setSex (Integer.parseInt (sexV.getText (). toString ()));
student.setAge (Integer.parseInt (ageV.getText (). toString ()));
student.setDangyuang (dangyuanV.getText (). toString (). equals ("1")? true: false);
student.setCreateTime (new Date ());
if (isnew) {
db.save (student);
} else {
db.update (student);
}
delete
Query (you still need to understand basic SQL)
List <Student> list = db.queryList (Student.class, ": name like? Or: num like?", "% Name%", "% num%");
: num already: the placeholder at the beginning indicates the object attribute (the num attribute here corresponds to num_no in the database)
In this way, you can use the object properties to query without understanding the database
? Placeholder refers to the data is replaced with the data behind the method supports multiple parameters
Those who understand spring jdbctemplete should like this way of writing
Adding, deleting, and modifying are all available, is it very useful?
dhroid-Dhdb orm simplifies SQLite database operation