2011-01-28 119 views
0

我有删除只有一行的问题。我可以插入和删除整个表。我无法理解整个ID的事情,以便删除一行,我正在看一些例子,我无法得到它。它让我疯狂,任何帮助都会非常感激。 这是sqlite类;从数据库中删除一行

public class datahelper { 
    private static final String DATABASE_NAME = "table.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String TABLE_NAME = "table1"; 

    private Context context; 
    private SQLiteDatabase db; 
    private SQLiteStatement insertStmt; 

    private static final String INSERT = 
     "insert into " + TABLE_NAME + "(name) values (?)"; 

    public datahelper(Context context) { 
     this.context = context; 
     OpenHelper openHelper = new OpenHelper(this.context); 
     this.db = openHelper.getWritableDatabase(); 
     this.insertStmt = this.db.compileStatement(INSERT); 
    } 

    public long insert(String name) { 
     this.insertStmt.bindString(1, name); 
     return this.insertStmt.executeInsert(); 
    } 

    public long insert2(String name) { 
      this.insertStmt2.bindString(1, name); 
      return this.insertStmt2.executeInsert(); 
    } 

    public void deleteAll() { 
     this.db.delete(TABLE_NAME, null, null); 
    } 

    private static class OpenHelper extends SQLiteOpenHelper { 
     OpenHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + TABLE_NAME + 
        " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)"); 
    } 
} 

回答

5

执行查询

DELETE FROM TABLE_NAME WHERE id = SOMEVALUE 
4

编辑:基于您的反馈,它看起来像你正在使用this API,它提供this delete method。我的猜测是,你可以这样做:

public void delete(int id) { 
    this.db.delete(TABLE_NAME, 'id = ?', new String[] { id.toString() }); 
} 

(原来的答复...)

使用与你要删除的ID只删除该行WHERE子句DELETE语句:

DELETE FROM <tablename> WHERE id = ? 

当然,你需要知道id为了做到这一点。 SQLite提供了一个函数 - sqlite3_last_insert_rowid() - 您可以在INSERT后立即调用。如果你的API不直接提供此功能,您可以通过相当于SQL function得到它间接:

SELECT last_insert_rowid() 

Alernatively如果你想删除某个名字(假设它是唯一的):

DELETE FROM <tablename> WHERE name = ? 
+0

什么令我困惑的是,我不能只输入该代码,我认为它必须看起来像这样public void delete(String id) { \t this.db.delete(TABLE_NUM,“id =?”,null); } – 2011-01-28 05:40:40