【问题标题】:Don't Insert record in `SQLite` table while syntax is correct语法正确时不要在“SQLite”表中插入记录
【发布时间】:2019-03-05 14:52:05
【问题描述】:

我有一个名为TABLE_MSG的表。我无法将记录插入其中。 insert 函数返回 -1 值。我在insert 函数中放置了几个log,并向我展示了我的模型正确传递给insert 函数并且ContentValues 具有价值并且不为空。似乎每件事都是正确的,但insert 不起作用。 这是我的模型。

public class message_model implements Serializable {

@SerializedName("id")
private int id;
@SerializedName("thread_id")
private int thread_id;
@SerializedName("user_id")
private int user_id;
@SerializedName("body")
private String body;
@SerializedName("created_at")
private String created_at;
@SerializedName("updated_at")
private String updated_at;
@SerializedName("deleted_at")
private String deleted_at;
// also I write getter and setter
}

这是创建表的查询。

String CREATE_TABLE_MSG = "CREATE TABLE " + TABLE_MSG + " ("
            + MSG_ID + " INTEGER PRIMARY KEY," + MSG_T_ID + " INTEGER," + MSG_U_ID + " INTEGER,"
            + MSG_BODY + " TEXT," + MSG_C_AT + " TEXT," + MSG_U_AT + " TEXT," + MSG_D_AT + " TEXT)";
    db.execSQL(CREATE_TABLE_MSG);

这是add_message 函数。

 public void add_message(message_model model) {
    try {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(MSG_ID,model.getId());
        values.put(MSG_T_ID, model.getThread_id());
        values.put(MSG_U_ID, model.getUser_id());
        values.put(MSG_BODY, model.getBody());
        values.put(MSG_C_AT, model.getCreated_at());
        values.put(MSG_U_AT, model.getUpdated_at());
        values.put(MSG_D_AT, model.getDeleted_at());
        Log.e("MESSAGE",values.toString()+" toString value \n");
        Log.e("MESSAGE",model.toString());
        long rowInserted= db.insert(TABLE_MSG, null, values);
        if(rowInserted != -1)
            Log.e("MESSAGE","IN INSERT MESSAGE TABLE and log is -1");
        else
            Log.e("MESSAGE","IN INSERT MESSAGE TABLE "+rowInserted);

        db.close();
    }
    catch (Exception e){
        Log.e("MESSAGE",e.toString());
    }
}

这是我调用add_message 函数。

message_model m=new message_model(589, 300, 400, "body", "createdat", "updatedat", null);
    db.add_message(m);

这是get_message函数。

public ArrayList<message_model> get_messages() {
    ArrayList<message_model> list = new ArrayList<message_model>();
    String selectQuery = "SELECT * FROM " + TABLE_MSG;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            message_model model = new message_model();
            model.setId(cursor.getInt(0));
            model.setThread_id(cursor.getInt(1));
            model.setUser_id(cursor.getInt(2));
            model.setBody(cursor.getString(3));
            model.setCreated_at(cursor.getString(4));
            model.setUpdated_at(cursor.getString(5));
            model.setDeleted_at(cursor.getString(6));
        }
        while (cursor.moveToNext());
    }

    cursor.close();
    db.close();
    Log.e("MESSAGE","in get message");
    Log.e("MESSAGE","in get message"+list.toString());
    return list;
}

这是我的电话get_message。

 ArrayList<message_model> listm = new ArrayList<>();
   listm = db.get_messages();
  for (int i = 0; i < listm.size(); i++) {
      Log.e("MESSAGE", "body in for " + listm.get(i).getBody());
       Log.e("MESSAGE", "id in for " + listm.get(i).getId() + "");
 }

【问题讨论】:

  • 尝试注释掉 values.put(MSG_ID,model.getId());,当 id 必须为 unqiue 时,您使用现有 id。不指定它将导致生成 id,因为该列是 rowid 的别名(即该列是用 INTEGER PRIMARY KEY 定义的)。
  • 当我从表中收到消息时,它返回一个空列表。没有任何内容不会插入到我的表中,因此没有重复的 id。
  • 然后暂时改用insertOrThrow方法代替insert方法。然后堆栈跟踪将显示潜在问题。
  • 你得到这个了吗:Log.e("MESSAGE","IN INSERT MESSAGE TABLE and log is -1");
  • @UmangBurman 是的。我收到了。

标签: android sqlite insert


【解决方案1】:

解决方案:

只需添加这一行:

list.add(model);

如下图(写在这里):

    if (cursor.moveToFirst()) {
    do {
        message_model model = new message_model();
        model.setId(cursor.getInt(0));
        model.setThread_id(cursor.getInt(1));
        model.setUser_id(cursor.getInt(2));
        model.setBody(cursor.getString(3));
        model.setCreated_at(cursor.getString(4));
        model.setUpdated_at(cursor.getString(5));
        model.setDeleted_at(cursor.getString(6));

        ......(Write Here)
    }
    while (cursor.moveToNext());
}

就是这样,您只是从数据库中获取数据,而不是将其添加到列表中

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多