实现效果对MainActivity界面的edit写数据库中对应的age,如果查询到就跳转到Main2Activity界面否则不跳转,在Main2Activity布局上edit写对应age然后跳转到MainActivity,输入之前写的age如果没有跳转证明删除成功。
思路
建立一个封装类对SQLiteOpenHelper的继承类管理
直接上代码
1.创建的一个继承SQLiteOpenHelper的类 mybase
public class myBASE extends SQLiteOpenHelper {
private static final String my_table="create table mytable(" +
"id integer primary key autoincrement," +
"name text," +
"age integer)";
private Context mContext;
public myBASE(Context context,String name,SQLiteDatabase.CursorFactory factory,int version){
super(context,name,factory,version);
mContext=context;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(my_table);
ContentValues values=new ContentValues();
values.put("name","tony");
values.put("age",16);
db.insert("mytable",null,values);
values.clear();
values.put("name","jack");
values.put("age",25);
db.insert("mytable",null,values);
}
}
2.创建管理mybase的类
public class base {
private static final String table="mytable.db";
private static final int version=1;
private static base base=null;
private SQLiteOpenHelper myBASE;
private base(Context context){
this.myBASE=new myBASE(context,table,null,version);
}
public static synchronized base setBase(Context context){
if (base==null){
base=new base(context);
}
return base;
}
public SQLiteOpenHelper getMyBASE(){
return this.myBASE;
}
public SQLiteDatabase getWrite(){
return myBASE.getWritableDatabase();
}
public SQLiteDatabase getread(){
return myBASE.getReadableDatabase();
}
}
3.MainActivity代码 布局就不写了 就一个button 和一个edit
public class MainActivity extends AppCompatActivity {
EditText text;
Button btn_start;
String string;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_start=(Button)findViewById(R.id.btn_start);
text=(EditText)findViewById(R.id.te_write);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
string=text.getText().toString();
Toast.makeText(MainActivity.this,string,Toast.LENGTH_SHORT).show();
SQLiteDatabase db=base.setBase(MainActivity.this).getWrite();
cursor=db.rawQuery("select * from mytable where age=?",new String[]{string});
if (cursor.moveToFirst()){
do {
String name=cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
}while (cursor.moveToNext());
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
cursor.close();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
4.Main2Activity代码 也是一个button 和一个edit
public class Main2Activity extends AppCompatActivity {
EditText editText;
Button btn_write;
String age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
init();
btn_write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
age=editText.getText().toString();
SQLiteDatabase db=base.setBase(Main2Activity.this).getWrite();
db.delete("mytable","age=?",new String[]{age});
Intent intent=new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent);
}
});
}
public void init(){
editText=(EditText)findViewById(R.id.write);
btn_write=(Button)findViewById(R.id.btn_delete);
} }
网友评论