Jake Alsemgeest
Jake Alsemgeest

Reputation: 702

Raw Query in Android SQLite - INSERT OR IGNORE

I'm having an issue inputting information into a Sqlite database on the app I'm creating. I was using the help of the Cursor before. I am used to MySQL although clearly not 'used to' that well.

I am trying to add to the database from a file. I had this working before but it would be added with the Cursor. I was then told that in order to make it so I could add new information to the file and have the app ONLY add the new information into the database I should use INSERT OR IGNORE.

Is this the correct syntax? I currently am not having any information inserted for whatever reason...

ourDatabase.rawQuery("INSERT OR IGNORE INTO " + DATABASE_TABLE + " ("+KEY_CLASS+",
 " + KEY_QUESTION+ ") VALUES ('" + qclass + "', '" + question + "');", null);

This is my database:

"CREATE TABLE " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    KEY_CLASS + " TEXT NOT NULL, " +
                    KEY_QUESTION + " TEXT NOT NULL UNIQUE);

Thanks for the help in advance!

Upvotes: 2

Views: 5252

Answers (1)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

Your query seems right but try the one below anyway

ContentValues insertValues = new ContentValues();
insertValues.put(KEY_CLASS, qclass);
insertValues.put(KEY_QUESTION, question);   
yourDbName.insertWithOnConflict(DATABASE_TABLE, null, insertValues, SQLiteDatabase.CONFLICT_IGNORE);  

KEY_QUESTION + " TEXT NOT NULL UNIQUE);";

Upvotes: 6

Related Questions