java - 无法将值插入到 android 中的 SQLite 数据库中

标签 java android sqlite android-cursor sqliteopenhelper

我有三个类(class)。一个是将 EditTexts 的文本发送到另一个类的 Activity :

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 类中的创建表命令:

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);");

最佳答案

logcat 说明了一切

TPCS_VESSEl.vessel_name 不能为空。看来您在创建时已将其声明为 not null 。所以最好设置一个默认值。 .或者在插入时给这个变量赋值

关于java - 无法将值插入到 android 中的 SQLite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17228538/

相关文章:

android - 如何使用 Jetpack Compose Navigation 处理弹出多个屏幕

sqlite - 无法找到 Mono.Data.Sqlite 库

java - 如何使用 Apache POI 获取 MS Excel 单元格的别名

java - 通过ActionListener更新JPanel内容

java - 从自定义应用程序启动百度 map Intent

java - NAT表过滤

java - Android ViewPager - 添加带有淡入/淡出动画的幻灯片以在 View 之间切换

Android 全屏同时仍然显示状态栏

android - 创建带有更新的新闻应用程序 android

sqlite - 如何对sqlite中的所有表使用一个序列