Mr.G
Mr.G

Reputation: 1275

Android SQL lite table insert issue

I have created a table called xxxx using android content provider. I un-installed the app and run and then when I try to insert data, I get no column found exception. I'll post my code below,

public class PMSQLGroupsMessage {
    public static final Integer version = 1;
    public static final String TABLE_NAME = "GroupMessage";

    public static final Uri CONTENT_URI = Uri.parse(PMSQLContentProvider.SCHEME
            + PMSQLContentProvider.AUTHORITY + "/" + TABLE_NAME);
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.hsware."
            + TABLE_NAME;
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.hsware."
            + TABLE_NAME;

    public static final String COL_ID = "_id";
    public static final String COL_GID = "gid";
    public static final String COL_PMID = "pmid";
    public static final String COL_READ = "read";
    public static final String COL_TYPE = "type";
    public static final String COL_DATE = "date";
    public static final String COL_MESSAGE = "_message";

    public static final String TABLE_CREATION = "CREATE TABLE IF NOT EXISTS  "
            + TABLE_NAME + " (" + COL_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_GID + " BIGINT,"
            + COL_PMID + " BIGINT," + COL_READ + " INTEGER," + COL_MESSAGE
            + "TEXT," + COL_TYPE + " INTEGER," + COL_DATE + " BIGINT);";

    Integer messageId;
    Long gid;
    Long pmid;
    Boolean read;
    Integer type;
    Date date;
    String _message;

    public Integer getMessageId() {
        return messageId;
    }

    public void setMessageId(Integer messageId) {
        this.messageId = messageId;
    }

    public Long getGid() {
        return gid;
    }

    public void setGid(Long gid) {
        this.gid = gid;
    }

    public Long getPmid() {
        return pmid;
    }

    public void setPmid(Long pmid) {
        this.pmid = pmid;
    }

    public Boolean getRead() {
        return read;
    }

    public void setRead(Boolean read) {
        this.read = read;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getMessage() {
        return _message;
    }

    public void setMessage(String message) {
        this._message = message;
    }

}

Exception :-

android.database.sqlite.SQLiteException: table GroupMessage has no column named _message (code 1): , while compiling: INSERT INTO GroupMessage(gid,_message,date) VALUES (?,?,?)

CODE:

public void addGroupMessages(long groupid, String message) {

        // save in GroupMessageTable
        ContentValues cv = new ContentValues();
        cv.put(PMSQLGroupsMessage.COL_DATE, new Date().getTime());
        cv.put(PMSQLGroupsMessage.COL_GID, groupid);
        cv.put(PMSQLGroupsMessage.COL_MESSAGE, message);

        cr.insert(PMSQLGroupsMessage.CONTENT_URI, cv);

    }

Upvotes: 0

Views: 100

Answers (3)

Kishan Dhamat
Kishan Dhamat

Reputation: 3784

Use this:

public static final String TABLE_CREATION = "CREATE TABLE IF NOT EXISTS  "
        + TABLE_NAME + " (" + COL_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_GID + " BIGINT,"
        + COL_PMID + " BIGINT," + COL_READ + " INTEGER," + COL_MESSAGE
        + " TEXT," + COL_TYPE + " INTEGER," + COL_DATE + " BIGINT);";

Upvotes: 1

Shailendra Madda
Shailendra Madda

Reputation: 21551

Replace this query:

public static final String TABLE_CREATION = "CREATE TABLE IF NOT EXISTS  "
            + TABLE_NAME + " (" + COL_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_GID + " BIGINT,"
            + COL_PMID + " BIGINT," + COL_READ + " INTEGER," + COL_MESSAGE
            + "TEXT," + COL_TYPE + " INTEGER," + COL_DATE + " BIGINT);";

with this:

public static final String TABLE_CREATION = "CREATE TABLE IF NOT EXISTS  "
            + TABLE_NAME + " (" + COL_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT," + COL_GID + " BIGINT,"
            + COL_PMID + " BIGINT," + COL_READ + " INTEGER," + COL_MESSAGE
            + " TEXT," + COL_TYPE + " INTEGER," + COL_DATE + " BIGINT);";

Note: You are missing space in front of TEXT.

Upvotes: 1

laalto
laalto

Reputation: 152827

In your CREATE TABLE, you're missing a space betweel COL_MESSAGE and its type TEXT.

After fixing that you probably need to uninstall your app so the old database file is removed.

Upvotes: 1

Related Questions