//进行查看数据
select.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取每行的结果集
Cursor cursor = database.rawQuery("select * from person",null);
//循环获取每一行的数据类型
while (cursor.moveToNext()) {
// 获取第一列的值 获取id行字段
int persnid = cursor.getInt(cursor.getColumnIndex("id"));
// 获取第二列的值
String name = cursor.getString(cursor.getColumnIndex("name"));
// 获取第三列的值
int age = cursor.getInt(cursor.getColumnIndex("age"));
// 获取第四列的值
String sex = cursor.getString(cursor.getColumnIndex("sex"));
//使用LogCat进行显示
Log.e("TAG", "数据库查询结果返回>>>"+"id:"+persnid+">>>"+"名字:"+name+">>>"+"年龄:"+age+">>>"+"性别:"+sex);
}
}
});
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
//获取编辑框的值
String person_name = name.getText().toString();
String person_age = age.getText().toString();
String person_sex = sex.getText().toString();
//判断 名字 年龄 性别不为空的时候进行 增删查改
if (person_name != null && !"".equals(person_name)
&& person_age != null && !"".equals(person_age)
&& person_sex != null && !"".equals(person_sex)) {
switch (v.getId()) {
//进行增加数据
case R.id.add:
//执行增加sql语句
database.execSQL("insert into person(name,age,sex) values ('"+person_name+"',"+person_age+",'"+person_sex+"')");
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
break;
//进行修改数据
case R.id.updata:
//根据名字修改年龄
database.execSQL("update person set age="+person_age+" where name ='"+person_name+"'");
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
break;
//进行删除数据
case R.id.delete:
database.execSQL("delete from person where name ='"+person_name+"'");
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
break;
}
}else {
//显示的时间比较短
Toast.makeText(MainActivity.this, "请不要输入空数据", Toast.LENGTH_SHORT).show();
}
}
select.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取每行的结果集
Cursor cursor = database.rawQuery("select * from person",null);
//循环获取每一行的数据类型
while (cursor.moveToNext()) {
// 获取第一列的值 获取id行字段
int persnid = cursor.getInt(cursor.getColumnIndex("id"));
// 获取第二列的值
String name = cursor.getString(cursor.getColumnIndex("name"));
// 获取第三列的值
int age = cursor.getInt(cursor.getColumnIndex("age"));
// 获取第四列的值
String sex = cursor.getString(cursor.getColumnIndex("sex"));
//使用LogCat进行显示
Log.e("TAG", "数据库查询结果返回>>>"+"id:"+persnid+">>>"+"名字:"+name+">>>"+"年龄:"+age+">>>"+"性别:"+sex);
}
}
});
}
OnClickListener l = new OnClickListener() {
@Override
public void onClick(View v) {
//获取编辑框的值
String person_name = name.getText().toString();
String person_age = age.getText().toString();
String person_sex = sex.getText().toString();
//判断 名字 年龄 性别不为空的时候进行 增删查改
if (person_name != null && !"".equals(person_name)
&& person_age != null && !"".equals(person_age)
&& person_sex != null && !"".equals(person_sex)) {
switch (v.getId()) {
//进行增加数据
case R.id.add:
//执行增加sql语句
database.execSQL("insert into person(name,age,sex) values ('"+person_name+"',"+person_age+",'"+person_sex+"')");
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
break;
//进行修改数据
case R.id.updata:
//根据名字修改年龄
database.execSQL("update person set age="+person_age+" where name ='"+person_name+"'");
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
break;
//进行删除数据
case R.id.delete:
database.execSQL("delete from person where name ='"+person_name+"'");
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
break;
}
}else {
//显示的时间比较短
Toast.makeText(MainActivity.this, "请不要输入空数据", Toast.LENGTH_SHORT).show();
}
}
};
//模糊查询
Cursor cursor =sqld.query("kunnanbiao", null, "name like ?", new String[]{"%"+quname+"%"}, null, null, null);