AndroidにおけるデータベースSQLiteのinsert挿入操作の理解


SQLiteの挿入操作insert方法の理解について
関数のプロトタイプ:
long android.database.sqlite.SQLiteDatabase.insert(String table, String nullColumnHack, ContentValues values)

パラメータは次のとおりです.
パラメータ
意味
String table
データを挿入するテーブルの名前
String nullColumnHack
valuesパラメータが空または中身がない場合、insertは失敗します(下位データベースでは空の行を挿入できません).これを防ぐために、ここで列名を指定します.挿入する行が空の行を見つけた場合、指定した列名の値をnullに設定し、データベースに挿入します.
ContentValues values
ContentValueオブジェクト、mapに似ています.キー値ペアの形式で値を格納します.
理解:String nullColumnHackの役割は、テーブル内のフィールド名(カラム名)を指定することです.下位データベースでは空の行を挿入できないため、valuesが空の場合、指定するカラムの値をnullに設定します.次のコードがあります.
insert into tableName()values(); //     ,    
insert into tableName (nullColumnHack)values(null); //     

実際には、下位レベルでは、さまざまなinsertメソッドが最後にinsertWithOnConflictメソッドを呼び出します.ここでは、このメソッドの実装の一部を貼り付けます.
[java] view plaincopy/** 
    * General method for inserting a row into the database. 
    * 
    * @param table the table to insert the row into 
    * @param nullColumnHack SQL doesn't allow inserting a completely empty row, 
    *            so if initialValues is empty this column will explicitly be 
    *            assigned a NULL value 
    * @param initialValues this map contains the initial column values for the 
    *            row. The keys should be the column names and the values the 
    *            column values 
    * @param conflictAlgorithm for insert conflict resolver 
    * @return the row ID of the newly inserted row 
    * OR the primary key of the existing row if the input param 'conflictAlgorithm' = 
    * {@link #CONFLICT_IGNORE} 
    * OR -1 if any error 
    */  
   public long insertWithOnConflict(String table, String nullColumnHack,  
           ContentValues initialValues, int conflictAlgorithm) {  
       if (!isOpen()) {  
           throw new IllegalStateException("database not open");  
       }  
  
       // Measurements show most sql lengths <= 152  
       StringBuilder sql = new StringBuilder(152);  
       sql.append("INSERT");  
       sql.append(CONFLICT_VALUES[conflictAlgorithm]);  
       sql.append(" INTO ");  
       sql.append(table);  
       // Measurements show most values lengths < 40  
       StringBuilder values = new StringBuilder(40);  
  
       Set> entrySet = null;  
       if (initialValues != null && initialValues.size() > 0) {  
           entrySet = initialValues.valueSet();  
           Iterator> entriesIter = entrySet.iterator();  
           sql.append('(');  
  
           boolean needSeparator = false;  
           while (entriesIter.hasNext()) {  
               if (needSeparator) {  
                   sql.append(", ");  
                   values.append(", ");  
               }  
               needSeparator = true;  
               Map.Entry entry = entriesIter.next();  
               sql.append(entry.getKey());  
               values.append('?');  
           }  
  
           sql.append(')');  
       } else {  
           sql.append("(" + nullColumnHack + ") ");  
           values.append("NULL");  
       }

ここでは、ContentValueタイプのデータinitialValueがnull、またはsize<=0の場合、sql文にnullColumnHackの設定が追加されます.ここでnullColumnHackを追加しないと、sql文の最終的な結果はinsert into tableName()values()に似ています.これは明らかに許されない.nullColumnHackを追加すると、sqlはinsert into tableName(nullColumnHack)values(null)になります.これでいいです.
最後に,この方法の4つのパラメータがそれぞれ表す意味と役割をコード注釈により理解できる.4番目のパラメータは次のとおりです.
    * @param conflictAlgorithm for insert conflict resolver 
    * @return the row ID of the newly inserted row 
    * OR the primary key of the existing row if the input param 'conflictAlgorithm' = 
    * {@link #CONFLICT_IGNORE} 

挿入操作が競合している場合、新しい挿入データのprimary keyが既存のデータprimary keyと同じようにどのように処理されるかを示します.