Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete a row in a database for android
    text
    copied!<p>I'm having a problem with my android code when I try to delete a row in my database with my delete method. However, I do not want to use the ID but rather use the name of the string(s) I create. The point is for a user to input a name in an edit text and have the string row be deleted. I am pretty new at this and would love your input! I'll take you through my code...</p> <p>Here is my Data </p> <pre><code>public class Key_Manager extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String CREATE_KEYS_TABLE = "CREATE TABLE " + KeyEntry.TABLE_NAME + " (" + KeyEntry.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KeyEntry.COLUMN_NAME_NAME + " TEXT," + KeyEntry.COLUMN_NAME_KEY + " TEXT" + ")"; public Key_Manager(Context context) { super(context,KeyEntry.DB_NAME, null, DATABASE_VERSION ); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_KEYS_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS" + KeyEntry.TABLE_NAME); onCreate(db); } public void addKey(String name, String key) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KeyEntry.COLUMN_NAME_NAME, name); values.put(KeyEntry.COLUMN_NAME_KEY, key); try{db.insertOrThrow(KeyEntry.TABLE_NAME, null, values);} catch(Exception e) { Log.e("Error in inserting rows", e.toString()); e.printStackTrace(); } db.close(); } public ArrayList&lt;String&gt; getKeys(int id) { ArrayList&lt;String&gt; listRows = new ArrayList&lt;String&gt;(); SQLiteDatabase db = this.getReadableDatabase(); String[] keys = new String[] { KeyEntry.KEY_ID, KeyEntry.COLUMN_NAME_NAME, KeyEntry.COLUMN_NAME_KEY }; Cursor cursor = db.query(KeyEntry.TABLE_KEY, keys, KeyEntry.KEY_ID + "=?", null, null, null, null); if(cursor !=null) { cursor.moveToFirst(); } while(cursor.isAfterLast()==false) { listRows.add(cursor.getString(0)); listRows.add(cursor.getString(1)); cursor.moveToNext(); } if(cursor != null &amp;&amp; !cursor.isClosed()) { cursor.close(); } return listRows; } public void onDownGrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); } public void deleteKey(String name) //THIS IS WHERE I AM HAVING PROBLEMS, WHAT SHOULD I DO HERE? { SQLiteDatabase db = this.getWritableDatabase(); db.delete(KeyEntry.TABLE_NAME, KeyEntry.COLUMN_NAME_NAME,new String[]{name}); } } </code></pre> <p>AND here is where I want to delete it </p> <pre><code>public class Delete_Keys extends Activity { private Key_Manager keyManager; EditText delete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delete__keys); delete = (EditText) findViewById(R.id.enterDelete); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.delete__keys, menu); return true; } public void deleteButton(View v)//AM I DOING THIS CORRECT? WHAT CAN I DO BETTER? { String name = delete.getText().toString(); keyManager = new Key_Manager(this); keyManager.deleteKey(name); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload