Android-xutils Framework Introduction (III)

Source: Internet
Author: User

Continue to introduce the last two modules of xutils: Dbutils and Httputils. First introduced the first SQLite database manipulation of the simple ORM Framework, as long as you can understand the xutils to provide us with the API, I believe you can also skillfully put dbutils into the project.

The tool class that manipulates the database, no matter how much ox X, is inseparable from the most fundamental crud, namely create, query, update and delete. The following is a brief description of how xutils is easy to persist data from these four angles. As we all know, in Android if you want to store an object, we need to create a sqliteopenhelper, and then we have to create a table corresponding to the individual properties of the object, we have to continue to convert our objects into contentvalues, and then to store. The real trouble is no more trouble, we now introduce the dbutils can let you easily free the same code. Dbutils in the save operation, the object will be reflected according to the Java reflection fields, and then go to query the database for the existence of the object type corresponding table, if the table already exists, the direct insert operation, if not exist, first dynamically created a table corresponding to our object, then insert processing. Directly on the code, you see.

@OnClick (R.id.insert) Public voidInsert (View v) {Student stu=NULL;  for(inti = 0; I < 20; i++) {Stu=NewStudent (); Stu.setage (10 +i); Stu.setname ("Jack" +i);            Mlist.add (Stu); Try{dbutils.save (stu); } Catch(dbexception e) {e.printstacktrace (); }        }    }

Note: Not all entity objects can be stored in this way quickly, so be sure to ensure that the object's type has an ID of type int or a _id attribute, which corresponds to the primary key field in the database table. If there is no ID field in the type, you can specify a field of type int as the primary key by @id annotations. If the fields in the table do not want to be stored in the database, they can be ignored by @transient. If you store a list of objects directly, this is also allowed to achieve the purpose of bulk storage.

Dbutils can help the students who are not very familiar with the SQL statement to implement the query quickly, rather than write SQL query statements, and can be sorted and paged query results, easy to use, powerful. As you can see, the following lines of code can implement complex query functions

Dbutils.findall (Selector.from (Student.  Class).        WHERE ("_id", "<", "ten"). and ("Age", ">", Ten). ("_id"        )* pageIndex)) ;

Similarly, the data in the database can be easily updated. The following shows the age field for the first record in the table that corresponds to update student. This is relatively simple, just go directly to the code to see it.

@OnClick (r.id.update)      Public void Update (View v) {        try  {            List<Student> stus = Dbutils.findall (Selector.from ( Student. class ));             = Stus.get (0);            Stu.setage (a);            Dbutils.update (Stu);         Catch (dbexception e) {            e.printstacktrace ();        }    }

The last one is the deletion of the data. An entity object, a set of entity objects, according to the conditions to delete, delete the table, delete the entire database, these operations can be implemented by a simple code, read the code to understand.

@OnClick (r.id.delete) Public voidDelete (View v) {Try{List<Student> Stus = Dbutils.findall (Selector.from (Student.class)); Dbutils.delete (Stus.get (0));            Dbutils.deleteall (Stus); Dbutils.deletebyid (Student.class, Wherebuilder.b ("age", "= =", 20)); Dbutils.droptable (Student.class);        Dbutils.dropdb (); } Catch(dbexception e) {e.printstacktrace (); }    }

Transferred from: http://my.oschina.net/jack1900/blog/174125

Android-xutils Framework Introduction (III)

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.