2013-06-21 96 views
1

我有三個類。一個是發送EditTexts的文本到其它類活動:無法在Android中插入值到SQLite數據庫中

String[] comments = new String[]{ number.getText().toString(), 
        vessel_name.getText().toString(), ... } //20 such values 
comment = datasource.createComment(comments); 

這是處理註釋的類:

private String[] allColumns = { MySQLiteHelper.COLUMN_ID, 
     MySQLiteHelper.COLUMN_IMO_NUMBER, 
     MySQLiteHelper.COLUMN_VESSEL_NAME,...} //21 such values 

public VesproModel createComment(String comment[]) { 
    long insertId = 0; 
    int i = 1, j = 0; 
    Cursor cursor = null; 
    ContentValues values = new ContentValues(); 
    values.put(allColumns[0],insertId); 
    for (; i < allColumns.length && j < comment.length ; i++ , j++) { 
     values.put(allColumns[i], comment[j]); 
     insertId = database.insert(MySQLiteHelper.TABLE_TPCS_VESSEL, null, values); 
     cursor = database.query(MySQLiteHelper.TABLE_TPCS_VESSEL, allColumns, 
       MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null); 
    } 

    cursor.moveToFirst(); 
    VesproModel newComment = cursorToComment(cursor); 
    cursor.close(); 
    return newComment; 
} 


private VesproModel cursorToComment(Cursor cursor) { 
    VesproModel comment = new VesproModel(); 
    Log.d("cursorToComment", "Before if cursorToComment"); 
    if(cursor != null && cursor.moveToFirst()){ 
     Log.d("cursorToComment", "inside cursorToComment"); 
    comment.setId(cursor.getLong(0)); 
    comment.setImo_number(cursor.getString(1)); 
    comment.setVessel_name(cursor.getString(2)); 
    comment.setVessel_type(cursor.getString(3)); 
     ... 
     ... 
     comment.setNo_engines(cursor.getInt(20)); 
    } 
    else Log.d("cursorToComment", "NULL evaluation"); 
    return comment; 
    } 

VesproModel包含變量及其getter和setter。 MySQLiteHelper類擴展了處理創建名爲TPC_VESSEL的表的查詢的SQLiteOpenHelper類。 是logcat的顯示一些錯誤消息:

06-21 10:45:03.826: E/SQLiteDatabase(24135): Error inserting _id=0 imo_number=YB 
06-21 10:45:03.826: E/SQLiteDatabase(24135): android.database.sqlite.SQLiteConstraintException: TPCS_VESSEl.vessel_name may not be NULL (code 19) 
06-21 10:45:03.826: E/SQLiteDatabase(24135): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) 
06-21 10:45:03.826: E/SQLiteDatabase(24135): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775) 
06-21 10:45:03.826: E/SQLiteDatabase(24135): at com.example.pcs.VesproDataSource.createComment(VesproDataSource.java:60) 
06-21 10:45:03.846: E/SQLiteDatabase(24135): Error inserting _id=0 imo_number=YB vessel_name=ego 
06-21 10:45:03.846: E/SQLiteDatabase(24135): android.database.sqlite.SQLiteConstraintException: TPCS_VESSEl.vessel_type may not be NULL (code 19) 
06-21 10:45:03.846: E/SQLiteDatabase(24135): at com.example.pcs.VesproDataSource.createComment(VesproDataSource.java:60) 
06-21 10:45:03.856: E/SQLiteDatabase(24135): Error inserting _id=0 vessel_type=Container imo_number=YB vessel_name=ego 

編輯1:MySQLiteHelper類create table命令:

database.execSQL("create table " 
      + TABLE_TPCS_VESSEL 
      + "("+ COLUMN_ID + " integer primary key autoincrement, " + 
        COLUMN_IMO_NUMBER +" text not null, " + 
        COLUMN_VESSEL_NAME + " text not null, " + 
        COLUMN_VESSEL_TYPE + " text not null, " + 
        COLUMN_SR_CERTIFICATE_NO +" text not null," + 
           ... 
           .... 
           COLUMN_NO_ENGINES + " integer not null);"); 

回答

3

的logcat的說,這一切

TPCS_VESSEl.vessel_name不能爲空。看起來你創建時聲明它不是null。所以最好設置一個默認值。 .Or雖然插入一個值給這個變量

+0

實際上我從editText發送vessel_name的值。還有其他錯誤也是這樣,告訴我其他字段也可能不爲空。 – no10downingstreet

+0

顯示您創建的表代碼 – stinepike

+0

我編輯了問題以包含create table命令。 – no10downingstreet

0

根據你的logcat,你插入空值vessel_name到數據庫中。並在表中設置NOT NULLvessel_name。所以最好在該列中插入值或爲其設置默認值。

0

好吧,我找到了解決方案。我搬到了database.insert和像這樣的外database.query命令for循環:

for (; i < allColumns.length && j < comment.length ; i++ , j++) { 
     values.put(allColumns[i], comment[j]); 
     } 

insertId = database.insert(MySQLiteHelper.TABLE_TPCS_VESSEL, null, values); 
cursor = database.query(MySQLiteHelper.TABLE_TPCS_VESSEL, allColumns, 
     MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null, null); 

我也談到了這一行:`

values.put(allColumns[0],insertId); 

爲insertId被設置爲自動增量。 `

相關問題