2010-11-25 23 views
2

我在我的Android项目中收到一个约束失败的异常,我无法弄清楚它来自哪里。我的助手类看起来是这样的:要输入在Android数据库中获取SQLiteContraintException

public static final String WORDS_TABLE = "words"; 
public static final String ATT_WORD = "word"; 
public static final String ATT_T1 = "T1"; 
public static final String ATT_T2 = "T2"; 
public static final String ATT_T3 = "T3"; 
public static final String ATT_T4 = "T4"; 
public static final String ATT_T5 = "T5"; 
public static final String ATT_RECENT = "recent"; 
public static final String ATT_RATING = "rating"; 

public static final String STATISTICS_TABLE = "statistics"; 
public static final String ATT_GAMESPLAYED = "gamesPlayed"; 
public static final String ATT_wordsCORRECT = "wordsCorrect"; 
public static final String ATT_wordsWRONG = "wordsWrong"; 
public static final String ATT_POINTTOTAL = "pointTotal"; 
public static final String ATT_WINS = "wins"; 
public static final String ATT_LOSSES = "losses"; 

public static final String SETTING_TABLE = "settings"; 
public static final String ATT_SID = "sid"; 
public static final String ATT_TYPE = "type"; 
public static final String ATT_DURATION = "duration"; 
public static final String ATT_PTSTOWIN = "ptsToWin"; 

public static final String CATEGORIES_TABLE = "categories"; 
public static final String ATT_CATEGORY = "category"; 

public static final String BELONGS_TABLE = "belongs"; 

public static final String USER_TABLE = "user"; 
public static final String ATT_UID = "uid"; 

public static final String DATABASE_NAME = "taboo"; 
private static final int DATABASE_VERSION = 1; 
private static final String TAG = "DBAdapter"; 

private static final String WORDS_CREATE = 
    "create table words (_id primary key autoincrement, word text, " 
    + "T1 text, T2 text, T3 text, T4 text, T5 text, " 
    + "recent integer, rating integer);"; 

private static final String CATEGORIES_CREATE = 
    "create table categories (_id integer primary key autoincrement, category text unique);"; 

private static final String BELONGS_CREATE =  
    "create table belongs (_id integer primary key autoincrement, word text, category text, " 
    + "foreign key (word) references words (word), foreign key (category) references categories (category));"; 

private static final String USER_CREATE = 
    "create table user (_id integer primary key autoincrement, uid text unique);"; 

private static final String STATISTICS_CREATE = 
    "create table statistics (_id integer primary key autoincrement, uid text unique, gamesPlayed integer, wordsCorrect integer, " 
    +"wordsWrong integer, pointTotal integer, wins integer, losses integer, " 
    +"foreign key (uid) references user (uid));"; 

private static final String SETTINGS_CREATE = 
    "create table settings (_id integer primary key autoincrement, uid text, sid text, " 
    +"type text, duration integer, ptsToWin integer, " 
    +"foreign key (uid) references user (uid));" 
    ; 


//************************************************************** 
//********************* Function Constants ******************** 
//************************************************************** 

private final Context context; 
private DatabaseHelper DBHelper; 
public static SQLiteDatabase db; 
private int wordCount = 100; 

//******************** Object 
public DBAdapter(Context ctx) 
{ 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 

private static class DatabaseHelper extends SQLiteOpenHelper 
{ 
    DatabaseHelper(Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     db.execSQL(WORDS_CREATE); 
     db.execSQL(CATEGORIES_CREATE); 
     db.execSQL(BELONGS_CREATE); 
     db.execSQL(USER_CREATE); 
     db.execSQL(STATISTICS_CREATE); 
     db.execSQL(SETTINGS_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
          int newVersion) 
    { 
     Log.w(TAG, "Upgrading database from version " + oldVersion 
       + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS " + WORDS_TABLE); 
     onCreate(db); 
    } 
}  

//**************************************************** 
//******** Database Functions ********************** 
//**************************************************** 

public DBAdapter open() throws SQLException 
{ 
    db = DBHelper.getWritableDatabase(); 
    return this; 
} 
public void close() 
{ 
    DBHelper.close(); 
} 

的使用了JExcelApi从Excel电子表格扫描数据我的数据库表“字”人口。填充方法如下所示:

public void WordPopulate(Context ctx) throws SQLException, BiffException, IOException 
{ 
    Workbook wb = null; 
    AssetManager asst = ctx.getAssets(); 

    wb = Workbook.getWorkbook(asst.open("words.xls")); 

    ContentValues content = new ContentValues(); 
    Sheet sh = wb.getSheet(0); 
    final int rows = sh.getRows() - 1; 
    final int columns = sh.getColumns() - 1; 

    Cell a1; 
    for (int i = 1; i <= rows; i++) { 
      for (int j = 1; j <= columns; j++) { 
       a1 = sh.getCell(j,i); 
       String str = a1.getContents(); 
       if (j == 1) content.put(DBAdapter.ATT_WORD, str); 
       if (j == 2) content.put(DBAdapter.ATT_T1, str); 
       if (j == 3) content.put(DBAdapter.ATT_T2, str); 
       if (j == 4) content.put(DBAdapter.ATT_T3, str); 
       if (j == 5) content.put(DBAdapter.ATT_T4, str); 
       if (j == 6) content.put(DBAdapter.ATT_T5, str); 
       if (j == 7) content.put(DBAdapter.ATT_RECENT, (int)0); 
       if (j == 8) content.put(DBAdapter.ATT_RATING, (int)0); 
      } 
      DBAdapter.db.insert(DBAdapter.WORDS_TABLE, "NULL", content); 
     } 
    } 

我认为电子表格中的所有数据都可以。有没有人有什么想法会导致SQLiteConstraintException?感谢您的时间

+0

PS - 它出现在循环中的插入语句的每个实例。下面是一个LogCat输出的例子 - 错误/数据库(28543):插入T4 = CITRUS T5 = PILLS字=维生素C等级= 0的错误T1 =营养最近= 0 T3 =橙色T2 = COLDS – meburbo 2010-11-25 02:21:25

回答

0

您的代码

private static final String WORDS_CREATE = 
    "create table words (_id primary key autoincrement, word text, " 
    + "T1 text, T2 text, T3 text, T4 text, T5 text, " 
    + "recent integer, rating integer);"; 

应该

private static final String WORDS_CREATE = 
    "create table words (_id integer primary key autoincrement, word text, " 
    + "T1 text, T2 text, T3 text, T4 text, T5 text, " 
    + "recent integer, rating integer);"; 

更正为 “_id 整数主键”。

不是错误,而是为了保持一致性,我觉得这

private static final String SETTINGS_CREATE = 
    "create table settings (_id integer primary key autoincrement, uid text, sid text, " 
    +"type text, duration integer, ptsToWin integer, " 
    +"foreign key (uid) references user (uid));" 
    ; 

应该

private static final String SETTINGS_CREATE = 
    "create table settings (_id integer primary key autoincrement, uid text, sid text, " 
    +"type text, duration integer, ptsToWin integer, " 
    +"foreign key (uid) references user (uid));"; 

变化是最后一个分号的位置。

相关问题