Six months of graduation., AndroidI've been studying for six months.,fragmented learned a little.,no purpose,so I looked at the Guo Go.<<first line of code>>,really feel good,for no system trainingAndroid, for,It 's a very good book.,forget who asked me how to learn.Android,Honestly, I don't know.,I'm a vegetable too .,But after reading this book ,,recommend to have a look at,This is a good information for beginners.,for people who have been exposed, it's good to have fun perfecting your knowledge system .,of course, no one has their own knowledge.,These are just my personal views..
Today we continue to speak Litepal Framework Source code Analysis , This blog is just analysis , More detailed move to the source to see .
1. modifying data using litepal
To modify a single piece of data :
Datasupport . Update (Modelclass, values, id); Modify database content by ID , let's look at his code.
public static synchronized int update (class<?> modelclass, contentvalues values, long id) {Updatehandler updatehand Ler = new Updatehandler (Connector.getdatabase ()); return Updatehandler.onupdate (Modelclass, ID, values);}
First get sqlitedatabase, then call the onUpdate method in Updatehandle , then let's continue to see the onUpdate method !
int OnUpdate (class<?> modelclass, long ID, contentvalues values) {if (values.size () > 0) {return mdatabase.update (Gettablename (Modelclass), values, "id =" + ID, null);} return 0;}
See the familiar code , Here is what you understand , using the traditional database update function
There is another way to update the database's individual data based on the ID :
news.update (2); entity object calls update method nonsense less say directly to see update source should be estimated. onupdate The code of the method is almost guess finished on the source
public synchronized int update (long id) {try {Updatehandler Updatehandler = new Updatehandler (Connector.getdatabase ()); int rowsaffected = Updatehandler.onupdate (this, id); Getfieldstosettodefault (). Clear (); return rowsaffected;} catch (Exception e) {throw new Datasupportexception (E.getmessage ());}}
As expected , or call the updatehandle method inside the onUpdate method , Only the parameters are not the same
int OnUpdate (Datasupport baseobj, long id) throws SecurityException, Illegalargumentexception,nosuchmethodexception, Illegalaccessexception, invocationtargetexception {list<field> supportedfields = GetSupportedFields ( Baseobj.getclassname ()); Contentvalues values = new Contentvalues ();p utfieldsvalue (baseobj, Supportedfields, values);p Utfieldstodefaultvalue ( Baseobj, values), if (values.size () > 0) {return mdatabase.update (Baseobj.gettablename (), values, "id =" + ID, null);} return 0;}
this Only see update, above two methods are mainly for value assignment The following sections are the contents of the traditional Operational database update
Above the main introduction of the database through the ID update a single piece of information , the following describes , A number of information updates , Of course, you can also update individual information using the following method .
public static synchronized int updateAll (class<?> modelclass, contentvalues values,string ... conditions) {return UpdateAll (Baseutility.changecase (Modelclass.getsimplename ()), values, conditions);}
Here through the ChangeCase method to convert the entity name to a string type of table name, how to implement to see the code itself, here skip, we continue to look at the call of the UpdateAll method
ublic static synchronized int updateAll (String tableName, contentvalues values,string ... conditions) {Updatehandler Updatehandler = new Updatehandler (Connector.getdatabase ()); return Updatehandler.onupdateall (TableName, values, conditions);}
Here again, call the Updatehandle method in the Onupdateall method, should be similar to the above OnUpdate
int Onupdateall (string tableName, contentvalues values, string ... conditions) {return doupdateallaction (TableName, values, conditions);}
Well, it's not the same, don't worry, we keep looking.
private int Doupdateallaction (string tableName, contentvalues values, String ... conditions) { Baseutility.checkconditionscorrect (conditions), if (values.size () > 0) {return mdatabase.update (TableName, values, Getwhereclause (conditions), Getwhereargs (conditions));} return 0;}
See your familiar code again, yes, that's the traditional way of manipulating the database. , this is just the way to continue the encapsulation
Of course update more than this one method, it also has several methods, we continue to see
public synchronized int UpdateAll (String ... conditions) {try {Updatehandler Updatehandler = new Updatehandler ( Connector.getdatabase ()); int rowsaffected = Updatehandler.onupdateall (this, conditions); Getfieldstosettodefault (). Clear (); return rowsaffected;} catch (Exception e) {throw new Datasupportexception (E.getmessage ());}}
Or that recipe, or that flavor, is not described here, using the entity object directly calling the UpdateAll method also uses the above code
2. Delete Data using Litepal
There are many ways to delete, of course, Guo Go also provided by the ID directly deleted, the following we will first delete data from the ID to start looking at the source code
public static synchronized int Delete (class<?> modelclass, long id) {int rowsaffected = 0; Sqlitedatabase db = Connector.getdatabase ();d b.begintransaction (); try {deletehandler deletehandler = new DeleteHandler (db); rowsaffected = Deletehandler.ondelete (modelclass, id);d b.settransactionsuccessful (); return rowsaffected;} finally {db.endtransaction ();}}
If you understand how the update is implemented, it should be easy to see the code above , get sqlitedatabase, then turn on the transaction, call the OnDelete method in the Deletehandle function to implement the deletion, if you have an impression of the above update data, You should have guessed that the OnDelete method uses the traditional operation database method to delete the data, the following code:
int OnDelete (class<?> modelclass, long id) {analyzeassociations (modelclass); int rowsaffected = Deletecascade ( Modelclass, id); rowsaffected + = Mdatabase.delete (Gettablename (Modelclass), "id =" + ID, NULL); Getforeignkeytabletodelete (). Clear (); return rowsaffected;}
notice here, above forgot to say, here ID is the main key, adding and removing changes to operate the foreign key, to ensure uniform data , the above code uses the traditional method of manipulating the database to delete data, the principle is similar to the above
Of course Litepal also supports the deletion of data based on conditions Datasupport.deleteall (modelclass, Conditions); Here's a look at the source code:
public static synchronized int deleteAll (class<?> modelclass, String ... conditions) {return DeleteAll ( Baseutility.changecase (Modelclass.getsimplename ()), conditions);}
As with the update, first convert the entity to a string -type table name , and then call the deleteAll method implementation to delete according to the condition
private int Dodeleteallaction (string tableName, string ... conditions) {Baseutility.checkconditionscorrect (conditions ); return Mdatabase.delete (TableName, getwhereclause (conditions), Getwhereargs (conditions));}
The same entity object called the Delete method is similar to the update
public static synchronized int Delete (class<?> modelclass, long id) {int rowsaffected = 0; Sqlitedatabase db = Connector.getdatabase ();d b.begintransaction (); try {deletehandler deletehandler = new DeleteHandler (db); rowsaffected = Deletehandler.ondelete (modelclass, id);d b.settransactionsuccessful (); return rowsaffected;} finally {db.endtransaction ();}}
If you understand the above, see this code you should be very happy, well, today is to pull so much, now you should be more understanding of the Litepal framework, the follow-up will be updated, there are questions about the message scatter ~ ~ ~
A brief analysis of the--litepal framework of the son of Aunt Guo (iii)