简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM