删除表中数据的两种⽅法(delete删)1.通过点击按钮来执⾏删除表中数据。(数据库和表的创建不在详细介绍,请⾃动阅读)
第⼀种⽅法:直接使⽤SQL来操作数据库,调⽤execSQL(sql)语句
public class MainActivity extends AppCompatActivity {
private Button mDeleteButton;
private MySqliteHelper mMySqliteHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mMySqliteHelper = Intance(MainActivity.this);
mDeleteButton = (Button) findViewById(R.id.delete_btn);
//点击按钮删除数据
mDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db = WritableDatabase();
String sql = "delete from "+Constant.TABLE_NAME+" where "+Constant.ID+" = 2 ";
db.close();
}
});
}
}
第⼆种⽅法:使⽤android API封装好的删除数据的操作
public class MainActivity extends AppCompatActivity {
private Button mDeleteButton;
private MySqliteHelper mMySqliteHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mMySqliteHelper = Intance(MainActivity.this);
mDeleteButton = (Button) findViewById(R.id.delete_btn);
//点击按钮删除数据
mDeleteButton.setOnClickListener(new View.OnClickListener() {
truncatedelete@Override
public void onClick(View view) {
db = WritableDatabase();
db.delete(Constant.TABLE_NAME,Constant.ID+" = ?",new String[]{"3"});
db.close();
}
});
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论