Al Phaba
Al Phaba

Reputation: 6755

Cannot insert values into my table in android and sqllite

my android app is crashing with this exception

12-02 12:40:50.145: E/AndroidRuntime(531): Caused by:android.database.sqlite.SQLiteException: near "(": syntax error: , while compiling: insert into target(username, (password, (lastLogin, (numberOfLogins, (status, (endpoint) values (?,?,?,?,?,?)

My insert statement in the code looks like this

private static final String INSERT = "insert into " + AttachmentTable.TABLE_NAME 
                                        + " (" + AttachmentColumns.STATUS + ", "
                                        + AttachmentColumns.RETRIES + ", "
                                        + AttachmentColumns.ATT_URI + ", "
                                        + AttachmentColumns.ATT_URI_SOURCE + ", "
                                        + AttachmentColumns.COMMENT+ ", "
                                        + AttachmentColumns.ADDED + ", "
                                        + AttachmentColumns.LAST_RETRY + ", "
                                        + AttachmentColumns.FINISHED + ") " +
                                        "values (?,?,?,?,?,?,?,?)";

In the code I tried to save one of the created attachments. But it does not work. In the Android File Explorer I could see that the database has been created. Here the save part from my code

DataManager data = new DataManagerImpl(this);
    Attachment att = new Attachment();
    att.setAdded("now");
    att.setAttUri("test");
    att.setAttUriSource("test");
    att.setComment("test");
    att.setLastRetry("test");
    att.setRetries(3);
    att.setStatus(0);
    data.setAttachment(att);

And here the setAttachment code

@Override
public boolean setAttachment(Attachment t) {
    boolean retVal = false;
    try{
        db.beginTransaction();
        long result = attDao.save(t);
        if (result != -1)
            retVal = true;

    }catch (SQLException e) {
        Log.e(TAG, "Exception during saving target " + e.getMessage() + " rolling back transaction");
    } finally{
        db.endTransaction();
    }
    return retVal; 

}

Upvotes: 0

Views: 339

Answers (2)

Mohsin Naeem
Mohsin Naeem

Reputation: 12642

to insert just do this

ContentValues values = new ContentValues();
    values.put(AttachmentColumns.STATUS, "hello");
    values.put(AttachmentColumns.ATT_URI, "uri");
             ...

and

db.insert(TABLE_NAME, null /* nullColumnHack */,
                values);

Upvotes: 2

PravinCG
PravinCG

Reputation: 7708

It seems your AttachmentColumns.STATUS and all other Strings contain "(" in them. Can you try to remove them.

Upvotes: 0

Related Questions