"Reprint" of the Android SQLite database insert operation

Source: Internet
Author: User
Tags sqlite database

Original post address: http://blog.csdn.net/jason0539/article/details/9977175

Prototype:

Long Android.database.sqlite.SQLiteDatabase.insert (string table, String nullcolumnhack, contentvalues values)

Parameter description:

table: Names of tables to insert data from

nullcolumnhack: When the values parameter is empty or there is no content in it, we will fail the insert (the underlying database does not allow inserting a blank line), in order to prevent this, we will specify a column name here, Then if you find that the behavior you want to insert is empty, the value of the column name that you specify is set to null and then inserted into the database.

values: A Contentvalues object, similar to a map. Stores the value in the form of a key-value pair.

Here a lot of people will be puzzled, nullcolumnhack exactly what to use, why will appear. When we do not set a column, it is not all the database to set the default value? Many fields set the default value is also null, the setting shown here is also null, what is the difference, how can it be displayed after the settings are allowed to insert it?

In fact, at the bottom, the various insert methods finally go back to call the Insertwithonconflict method, here we paste out the partial implementation of the method:

1[Java] View plaincopy/** 2 * General method for inserting a row into the database.3     * 4     * @paramtable The table to insert the row into5     * @paramnullcolumnhack SQL doesn ' t allow inserting a completely empty row,6 * So if initialvalues are empty this column would explicitly be7 * Assigned a NULL value8     * @paraminitialvalues This map contains the initial column values for the9 * row. The keys should be the column names and the values theTen * Column Values One     * @paramconflictalgorithm for insert Conflict Resolver A     * @returnThe row ID of the newly inserted row - * OR The primary key of the existing row if the input param ' conflictalgorithm ' = -     * {@link#CONFLICT_IGNORE} the * OR-1 if any error -     */   -     Public Longinsertwithonconflict (String table, String nullcolumnhack, -Contentvalues Initialvalues,intconflictalgorithm) {   +        if(!IsOpen ()) {   -            Throw NewIllegalStateException ("Database not open");  +        }   A    at        //measurements Show Most SQL lengths <= -StringBuilder sql =NewStringBuilder (152);  -Sql.append ("INSERT");  - sql.append (Conflict_values[conflictalgorithm]);  -Sql.append ("into");  - sql.append (table);  in        //measurements Show most values lengths < -StringBuilder values =NewStringBuilder (40);  to    +set<map.entry<string, object>> entryset =NULL;  -        if(Initialvalues! =NULL&& initialvalues.size () > 0) {   theEntrySet =Initialvalues.valueset ();  *iterator<map.entry<string, object>> entriesiter =Entryset.iterator ();  $Sql.append (' ('); Panax Notoginseng    -            BooleanNeedseparator =false;  the             while(Entriesiter.hasnext ()) { +                if(needseparator) { ASql.append (",");  theValues.append (",");  +                }   -Needseparator =true;  $map.entry<string, object> Entry =Entriesiter.next ();  $ Sql.append (Entry.getkey ());  -Values.append ('? '));  -            }   the    -Sql.append (') '); Wuyi}Else {   theSql.append ("(" + Nullcolumnhack + ")");  -Values.append ("NULL");  Wu}

Here we can see that when the data of our contentvalues type is initialvalues to null, or size<=0, the Nullcolumnhack setting is added to the SQL statement. We can imagine that if we do not add nullcolumnhack, then the final result of our SQL statement will be similar to insert into TableName () values (), which is obviously not allowed. And if we add the Nullcolumnhack, then SQL will turn into this, insert into TableName (nullcolumnhack) VALUES (null), which is obviously possible.

"Reprint" The Android SQLite database insert operation

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.