1

这是我的表创建命令:

private static final String PINGPONG_CREATE = "create table " + TABLE_PINGPONG + "(" + COLUMN_ID_PINGPONG + " integer primary key autoincrement, " + COLUMN_CNAME +
            " text not null, " + COLUMN_DATE + " text not null, " + " text not null, " + COLUMN_LEAD + " numeric, " + COLUMN_PLACE + " text not null, " 
            + COLUMN_SET_ONE1 + " numeric, " + COLUMN_SET_ONE2 + " numeric, " + COLUMN_SET_TWO1 + " numeric, " + COLUMN_SET_TWO2 + " numeric, " + COLUMN_SET_THREE1
             + " numeric, " + COLUMN_SET_THREE2 + " numeric, " + COLUMN_SET_FOUR1 + " numeric, " + COLUMN_SET_FOUR2 + " numeric, " + COLUMN_SET_FIVE1  + " numeric, " 
            + COLUMN_SET_FIVE2 + " numeric, " + COLUMN_SET_SIX1 + " numeric, " + COLUMN_SET_SIX2 + " numeric, " + COLUMN_SET_SEVEN1 + " numeric, " + COLUMN_SET_SEVEN2
             + " numeric, " + COLUMN_RESULT1 + " numeric, " + COLUMN_RESULT2 + " numeric);"; 

这是我在数据库中输入“PingPongeEntry”的方法

public void createPingPongEntry(int lead, String challengeName, String date, String place, int set11, int set12, int set21, int set22,
            int set31, int set32, int set41, int set42, int set51, int set52, int set61, int set62, int set71, int set72, int tot1, int tot2){
        ContentValues values = new ContentValues();
        values.put(CustomSQLiteHelper.COLUMN_LEAD, lead);
        values.put(CustomSQLiteHelper.COLUMN_CNAME, challengeName);
        values.put(CustomSQLiteHelper.COLUMN_DATE, date);
        values.put(CustomSQLiteHelper.COLUMN_PLACE, place);
        values.put(CustomSQLiteHelper.COLUMN_SET_ONE1, set11);
        values.put(CustomSQLiteHelper.COLUMN_SET_ONE2, set12);
        values.put(CustomSQLiteHelper.COLUMN_SET_TWO1, set21);
        values.put(CustomSQLiteHelper.COLUMN_SET_TWO2, set22);
        values.put(CustomSQLiteHelper.COLUMN_SET_THREE1, set31);
        values.put(CustomSQLiteHelper.COLUMN_SET_THREE2, set32);
        values.put(CustomSQLiteHelper.COLUMN_SET_FOUR1, set41);
        values.put(CustomSQLiteHelper.COLUMN_SET_FOUR2, set42);
        values.put(CustomSQLiteHelper.COLUMN_SET_FIVE1, set51);
        values.put(CustomSQLiteHelper.COLUMN_SET_FIVE2, set52);
        values.put(CustomSQLiteHelper.COLUMN_SET_SIX1, set61);
        values.put(CustomSQLiteHelper.COLUMN_SET_SIX2, set62);
        values.put(CustomSQLiteHelper.COLUMN_SET_SEVEN1, set71);
        values.put(CustomSQLiteHelper.COLUMN_SET_SEVEN2, set72);
        values.put(CustomSQLiteHelper.COLUMN_RESULT1, tot1);
        values.put(CustomSQLiteHelper.COLUMN_RESULT2, tot2);

        long insertID = database.insert(CustomSQLiteHelper.TABLE_PINGPONG, null, values);

        Cursor cursor = database.query(CustomSQLiteHelper.TABLE_PINGPONG, allColumns3, CustomSQLiteHelper.COLUMN_ID_RUNNING + " = " + insertID, null, null, null, null);
        cursor.moveToFirst();
        cursor.close();
    }

这就是 allColums3-String[]

private String[] allColumns3 = {CustomSQLiteHelper.COLUMN_ID_PINGPONG, CustomSQLiteHelper.COLUMN_CNAME, CustomSQLiteHelper.COLUMN_DATE, CustomSQLiteHelper.COLUMN_PLACE, CustomSQLiteHelper.COLUMN_LEAD,
                                    CustomSQLiteHelper.COLUMN_SET_ONE1,
                                    CustomSQLiteHelper.COLUMN_SET_ONE2, CustomSQLiteHelper.COLUMN_SET_TWO1, CustomSQLiteHelper.COLUMN_SET_TWO2, CustomSQLiteHelper.COLUMN_SET_THREE1, CustomSQLiteHelper.COLUMN_SET_THREE2,
                                    CustomSQLiteHelper.COLUMN_SET_FOUR1, CustomSQLiteHelper.COLUMN_SET_FOUR2, CustomSQLiteHelper.COLUMN_SET_FIVE1, CustomSQLiteHelper.COLUMN_SET_FIVE2, CustomSQLiteHelper.COLUMN_SET_SIX1,
                                    CustomSQLiteHelper.COLUMN_SET_SIX2, CustomSQLiteHelper.COLUMN_SET_SEVEN1, CustomSQLiteHelper.COLUMN_SET_SEVEN2, CustomSQLiteHelper.COLUMN_RESULT1, CustomSQLiteHelper.COLUMN_RESULT2};

现在在线long insertID = database.insert(CustomSQLiteHelper.TABLE_PINGPONG, null, values);我得到了 SQLiteConstraintException,我不知道为什么。它说:

09-29 19:55:13.151: E/Database(1609): Error inserting set51=0 set52=0 set41=3 set61=0 lead=0 set72=0 set71=0 set62=0 date=29-09-2012 result1=4 result2=0 name=pingpong set32=1 set31=3 set42=1 place=hier set22=1 set21=3 set12=1 set11=3

(我在这里也很困惑,为什么没有插入值的顺序)

我在这里阅读了一些关于这个主题的问题,但是所有的建议都遵循:主键是自动递增的,而且此时我试图插入记录,数据库是空的。

我有 2 种插入方法,FootBallEntrysGames以此类比,一切正常。那么为什么它不在这里创建一个唯一的 uid 呢?

4

1 回答 1

0

如果你得到一个 ConstraintException,你就违反了表中的一些约束。您拥有的唯一规则是“主键”,它在后台创建“唯一”约束。确保您没有尝试在“COLUMN_ID_PINGPONG”列中插入已经存在的值。

于 2012-09-29T20:19:41.573 回答