DHROID-DHDB ORM simplifies SQLite database operations

Source: Internet
Author: User
Tags gettext sqlite sqlite database


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.