我有三节课。一个是将EditTexts的文本发送到另一个类的活动:
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类扩展了SQLiteOpenHelper类,该类处理创建名为TPC_VESSEL的表的查询。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);");发布于 2013-06-21 13:56:06
logcat说明了一切
TPCS_VESSEl.vessel_name不能为null。似乎您在创建时已将其声明为非null。所以最好设置一个默认值。向此变量插入put值时的.Or
发布于 2013-06-21 14:02:02
根据您的日志,您正在将vessel_name的空值插入数据库。然后在表中为vessel_name设置NOT NULL。因此最好是在OR中为该列设置默认值。
发布于 2013-06-21 18:11:59
好了,我找到了解决方案。我将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被设置为自动递增。`
https://stackoverflow.com/questions/17228538
复制相似问题