How do I delete a sqlite table row entry by string?

android, database, exception, sqlite, string

Solution

You should not use the string directly and if you do you need to wrap it in `'` since SQLite will consider it a column name otherwise.

The safest way to do it is

public void deleteEntry(String coin) throws SQLException{
    String[] whereArgs = new String[] { coin };
    ourDatabase.delete(DATABASE_TABLE, KEYNAME + "=?", whereArgs);
}

The unsafe way is

ourDatabase.delete(DATABASE_TABLE, KEYNAME + "='" + coin + "'", null);

and that will fail if `coin` contains a `'` so don't use it.

Problem

I'm a noob to Android, and I am trying to figure out how to delete a table row in sqlite with by a string as opposed to a long for the row number. I have no issues adding data to my table, but when I try to delete the same value I get a SQL exception that says `SQLiteException: no such column 'my_entry': while compiling: DELETE FROM dbTABLE WHERE db_NAME = my_entry` Any insight is greatly appreciated. Here is my code: Code for adding row entry: ``` public long createEntry(String coin, String quantity, String value) { ContentValues cv = new ContentValues(); cv.put(KEY_NAME, coin); cv.put(KEY_QUANTITY, quantity); cv.put(KEY_VALUE, value); return ourDatabase.insert(DATABASE_TABLE, null, cv); } ``` Code for deleting row entry: ``` public void deleteEntry(String coin) throws SQLException{ ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=" + coin, null); } ``` Code for entire Database: ``` public static final String KEY_ROWID = "_id"; public static final String KEY_NAME = "cointype_name"; public static final String KEY_QUANTITY = "cointype_quantity"; public static final String KEY_VALUE = "cointype_value"; private static final String DATABASE_NAME = "PortfolioDatabase"; private static final String DATABASE_TABLE = "cointypeTable"; private static final int DATABASE_VERSION = 1; private DbHelper ourHelper; private final Context ourContext; private SQLiteDatabase ourDatabase; private static class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_QUANTITY + " TEXT NOT NULL, " + KEY_VALUE + " TEXT NOT NULL);" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); onCreate(db); } } public PortfolioDatabase(Context c){ ourContext = c; } public PortfolioDatabase open() throws SQLException{ ourHelper = new DbHelper(ourContext); ourDatabase = ourHelper.getWritableDatabase(); return this; } public void close(){ ourHelper.close(); } public long createEntry(String coin, String quantity, String value) { ContentValues cv = new ContentValues(); cv.put(KEY_NAME, coin); cv.put(KEY_QUANTITY, quantity); cv.put(KEY_VALUE, value); return ourDatabase.insert(DATABASE_TABLE, null, cv); } public String getData() { String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE }; Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); String result = ""; int iRow = c.getColumnIndex(KEY_ROWID); int iName = c.getColumnIndex(KEY_NAME); int iQuantity = c.getColumnIndex(KEY_QUANTITY); int iValue = c.getColumnIndex(KEY_VALUE); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iQuantity) + " " + c.getString(iValue) + "\n"; } return result; } public String getName(long l) throws SQLException{ String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE}; Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null); if (c != null){ c.moveToFirst(); String name = c.getString(1); return name; } return null; } public String getHotness(long l) throws SQLException{ String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE }; Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null); if (c != null){ c.moveToFirst(); String hotness = c.getString(2); return hotness; } return null; } public void updateEntry(String mCoin, String mQuantity, String mValue) throws SQLException { ContentValues cvUpdate = new ContentValues(); cvUpdate.put(KEY_NAME, mCoin); cvUpdate.put(KEY_QUANTITY, mQuantity); cvUpdate.put(KEY_VALUE, mValue); ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + mCoin, null); } public void deleteEntry(String coin) throws SQLException { ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=" + coin, null); } } ```

Original source