主键不起作用

时间:2014-08-11 14:07:08

标签: android sqlite insert

我是adroid的新手。我创建了一个这样的表:

public class DBHelper extends SQLiteOpenHelper {

    public String DBPath;
    public static String DBName = "FAEXPED";
    public static final int version = 1;    
    public static String tableName = "LesOrdres";

    public static final String KEY_CodeClient = "CodeClient";
    public static final String KEY_CodeDest = "CodeDestinataire";
    public static final String KEY_NoOrdre = "NoOrdre";
    public static final String KEY_Ordre_1 = "Ordre_1"; 
    public static final String KEY_Ordre_2 = "Ordre_2"; 
    public static final String KEY_DateOrdre = "DateOrdre";
    public static final String KEY_GPSOrdre = "GPSOrdre";       

    private static final String TAG = "Create_The_DB";

    private static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
            tableName + "( "+ KEY_CodeClient + " VARCHAR, "+ KEY_CodeDest +"  VARCHAR," + KEY_NoOrdre +"  VARCHAR,"+ KEY_Ordre_1 +"  VARCHAR,"+ KEY_Ordre_2 +"  VARCHAR, "
                            + KEY_DateOrdre + " DATE, "+ KEY_GPSOrdre +"  VARCHAR,"     
             + " PRIMARY KEY("+KEY_CodeClient+","+ KEY_NoOrdre+","+KEY_Ordre_1+","+KEY_DateOrdre+","+KEY_GPSOrdre+"))";

    public DBHelper(Context context) {
        //super(context, name, factory, version);
        // TODO Auto-generated constructor stub

        super(context, DBName, null, version);      
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      Log.w(TAG, TABLE_CREATE);   
      db.execSQL(TABLE_CREATE);   
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
         Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                 + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + tableName);
        onCreate(db);

    }
}

和我的SQLControll:

public long createItmes(Pers_Ordre ItmOrdre) {

      ContentValues initialValues = new ContentValues();
      initialValues.put(DBHelper.KEY_CodeClient, ItmOrdre.getLe_CodeClient());
      initialValues.put(DBHelper.KEY_CodeDest, ItmOrdre.getLe_CodeDest());
      initialValues.put(DBHelper.KEY_NoOrdre, ItmOrdre.getLe_NoOrdre());
      initialValues.put(DBHelper.KEY_Ordre_1, ItmOrdre.getLe_Ordre_1());
      initialValues.put(DBHelper.KEY_Ordre_2, ItmOrdre.getLe_Ordre_2());
      initialValues.put(DBHelper.KEY_DateOrdre, ItmOrdre.getLe_Date());
      initialValues.put(DBHelper.KEY_GPSOrdre, ItmOrdre.getLe_GPS());

      return database.insert(DBHelper.tableName, null, initialValues);
     }

和通过扫描插入数据的主要活动:

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        //retrieve scan result
        IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

        if (scanningResult != null) {
            //we have a result
            String scanContent = scanningResult.getContents();
            if (scanContent != null)
            {
                String[] TempOrdre = scanContent.split("_");

                dbcon.open();
                Pers_Ordre oOrdre = new Pers_Ordre();
                oOrdre.setLe_CodeClient(TempOrdre[0]);
                oOrdre.setLe_CodeDest(TempOrdre[1]);
                oOrdre.setLe_NoOrdre(TempOrdre[2]);
                oOrdre.setLe_Ordre_1(TempOrdre[3]);
                oOrdre.setLe_Ordre_2(TempOrdre[4]); 
                oOrdre.setLe_Date(LedateFormat.format(Ledate));
                oOrdre.setLe_GPS(TempAddr);

                dbcon.createItmes(oOrdre);
                dbcon.close(); 
            }
            else
            {
                finish();
//              Intent GoToMain = new Intent(this, MainActivity.class);
//              startActivity(GoToMain);
            }


        }
        else{           
            Toast toast = Toast.makeText(getApplicationContext(), 
                "No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

你可以看到我有5个主键。 问题:

  1. 我可以将值null插入KEY_GPSOrdre,这很奇怪,因为我知道主键不允许空值

  2. 我可以插入相同的键值(double),通常它会拒绝它。

  3. 我的编辑方式如下:

    private static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
                tableName + "( "+ KEY_CodeClient + " VARCHAR, "+ KEY_CodeDest +"  VARCHAR," + KEY_NoOrdre +"  VARCHAR,"+ KEY_Ordre_1 +"  VARCHAR,"+ KEY_Ordre_2 +"  VARCHAR, "
                                + KEY_DateOrdre + " DATE, "+ KEY_GPSOrdre +"  VARCHAR,"     
                 + " PRIMARY KEY("+KEY_CodeClient+","+ KEY_NoOrdre+","+KEY_Ordre_1+","+KEY_DateOrdre+","+KEY_GPSOrdre+")"
                 + " UNIQUE     ("+KEY_CodeClient+","+ KEY_NoOrdre+","+KEY_Ordre_1+","+KEY_DateOrdre+","+KEY_GPSOrdre+") ON CONFLICT REPLACE)";
    

    但我仍然可以创造双倍。 我的数据是:

    Code Client  NoORdre    Key1Ordre   DateOrdre             GPSOrdre
    1406         2555       1           11/08/2014 16:30      NULL
    1406         2555       1           11/08/2014 16:30      NULL
    

    我再次编辑:

    private static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " +
                tableName + "( "+ KEY_CodeClient + " VARCHAR, "+ KEY_CodeDest +"  VARCHAR," + KEY_NoOrdre +"  VARCHAR,"+ KEY_Ordre_1 +"  VARCHAR,"+ KEY_Ordre_2 +"  VARCHAR, "
                                + KEY_DateOrdre + " DATE, "+ KEY_GPSOrdre +"  VARCHAR,"     
                 + " PRIMARY KEY("+KEY_CodeClient+","+ KEY_NoOrdre+","+KEY_Ordre_1+","+KEY_DateOrdre+","+KEY_GPSOrdre+")"
                 + " UNIQUE     ("+KEY_CodeClient+","+ KEY_NoOrdre+","+KEY_Ordre_1+","+KEY_DateOrdre+","+KEY_GPSOrdre+") ON CONFLICT REPLACE)";
    
        private static final String UNIQUE_INDEX = "CREATE UNIQUE INDEX idx_Ordre ON "+tableName+
                              "("+KEY_CodeClient+","+ KEY_NoOrdre+","+KEY_Ordre_1+","+KEY_DateOrdre+","+KEY_GPSOrdre+")";
    
    @Override
        public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          Log.w(TAG, TABLE_CREATE);   
          db.execSQL(TABLE_CREATE);
          db.execSQL(UNIQUE_INDEX);
        }
    

    但仍然不起作用,我仍然加倍。

1 个答案:

答案 0 :(得分:0)

您没有五个主键,您有一个由五列组成的复合主键。

要插入NULL,请考虑following

  

具有主键的表中的每一行必须在其主键列中具有唯一的值组合。为了确定主键值的唯一性,NULL值被认为与所有其他值不同,包括其他NULL。

为了插入相同的值,它是组成复合键的所有列的组合。

如果要将列限制为唯一值,请对其使用UNIQUE约束。