I have explained how to use the execsql () and rawquery () methods in SQLite in Android to implement curd operations. However, these operations are performed when we are very familiar with SQLite, however, sometimes we cannot avoid having different operation habits and may provide more convenient operation procedures for beginners during the operation. Therefore, Android is relatively human-friendly, for example, it is used to pack SQL for some primary users. The primary users only need to set and implement simple parameters to complete the normal SQL curd operation, for example, Android provides four methods: oninsert (), onupdate (), onquery (), and ondelete (). The following describes the methods in detail.
The first is the insert () method: sqlitedatabase DB = new dbopenhelper (). getwritabledatabae (); dB. insert (string table, string nullcolumnhack, contentvalues values); In this method, the first parameter is the name of the table to be filled in, and the second parameter is the processing method when the column is empty, the third parameter is used to store the values of each field, similar to map used to store name-value pairs. The detailed operation can be as follows: when we have an object parameter named contact, the contact field names are: ID is the primary key, name, phone. First open the database, sqlitedatabase DB = new dbopenhelper (). getwritabledatabase (), contentvalues values = new contentvalues (); values. put ("name", contact. getname), values. put ("phone", contact. getphone); dB. insert ("Contact", null, values );
The second is the use of the update () method: Also sqlitedatabase DB = new dbopenhelper (). getwritabledatabase () to open the database, and then use contentvalues values = new contentvalues (); values. put ("name", contact. getname (); values. put ("phone", contact. getphone (); dB. update (string table, contentvalues values, string whereclause, string [] whereargs); that is, the first parameter is the table name, the second parameter is the updated field name, and the third parameter is, the condition for its operation. The fourth parameter is the required condition value based on the third parameter. The specific operation is as follows: sqlitedatabase DB = new dbopenhelper (). Getwritabledatabasae (); contentvalues values = new contentvalues (); values. put ("name", contact. getname (); values. put ("phone", contact. getphone (); dB. update ("Contact", values, "id =? ", New string [] {string. valueof (contact. GETID ())});
Query () operation: In the query operation, we need to operate its cursor, so we hope we are not familiar with the database SQL cursor, I will go online and check it again. I will not introduce it more here: DB. query (string table, string [] columns, string selection, string [] selectionargs, string groupby, string having, string orderby); similarly, the first parameter is the table name, the second parameter is the column name, that is, the field. The third parameter is the condition, the fourth parameter is the condition value, the fifth parameter is the grouping operation, and the sixth parameter is the filtering operation, the seventh parameter is the sorting operation. For example: sqlitedatabase DB = dbopenhelper. getreadabledatabase (); cursor = dB. query ("Contact", null, "id = ?, New String [] {ID. tostring ()} ", null, null," id ASC "); If (cursor. movetofirst () {int id = cursor. getint (cursor. getcolumnindex ("ID"); string name = cursor. getstring (cursor. getcolumni, ndex ("name"); string phone = cursor. getstring (cursor. getcolumnindex ("phone"); return new contact (ID, name, phone) ;}, the query statement is complete here, isn't it easy.
The last step is the operation of the delete () method: it is simpler: sqlitedatabase DB = new dbopenhelper (). getwritabledatabase (); dB. delete (string table, string whereclause, string [] selectionargs): sqlitedatabase DB = new dbopenhelper (). getwritable (); dB. delete ("Contact", "id =? ", New string [] {ID. tostring ()});
Well, the above is a simple SQL curd operation provided for beginners in Android. I hope I can find more information on the Internet to solve this problem, of course, it not only provides these methods, such as Replace (), but here I need to note that this basic operation should not be used as far as possible, because it is an operation after SQL encapsulation, although we do not need to write some details about SQL statements, it still has a great impact on the execution of a complete SQL statement, in general, the direct consequences of encapsulation will lead to unnecessary function addition and Data Execution execution cycles, which will ultimately affect the performance of Data Execution. Therefore, I still mind using execsql () and rawquery () in the source code, we can see the following section: stringbuilder sb = new stringbuilder (521); sb. append ("insert"); If (algorithm! = NULL) {SQL. append ("or"); SQL. append (algorithm. value (); SQL. append ("into"); SQL. append (table) ;}. for example, if we use the insert () method, does its values have a value? nullcolumnhack will add a parameter to us, when the third parameter is null or the set does not have any elements, the system uses the second parameter by default to construct a complete SQL statement. If the second parameter is empty, there is still a problem with this syntax. It is also impossible to set the second parameter as the primary key because it is empty by default. However, the primary key cannot be empty, only SQLite thinks it is possible, because when you set it as the primary key, its default null value will not work, and it will automatically grow and take effect, and its name can be retrieved at will, OK here, the operation on the basic query method of SQLite is also finished. I may not explain these operations in a very sequential manner, because these are simple summaries from a long time ago, so here I feel that I can post it when I have time to give some reference to those who love programming ...... if you have any questions, please leave a message at any time