android 增,改,删

Model实体类:

public class Student {
	private int sid;
	private String name;
	private int age;
	
	public Student(){
		super();
	}
	public Student(int sid,String name,short age){
		super();
		this.sid=sid;
		this.name=name;
		this.age=age;
	}
	
	public int getSid()  {
		return sid;
	}
	public void setSid(int sid){
		this.sid=sid;
	}
	
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name=name;
	}
	
	public void setAge(int age){
		this.age=age;
	}
	public int getAge() {
		return age;
	}
	@Override
	public String toString(){
		return "sid=" + sid + ";name=" + name + ";age=" + age;
	}

}

 helper帮助类:

public class Helper extends SQLiteOpenHelper {
	private static final int VERSION=1;
	private static final String DBMAME="data.db";
	public Helper(Context context) {
		super(context, DBMAME,null,VERSION);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE t_student (sid INTEGER PRIMARY key autoincrement,name VARCHAR(20),age integer)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//版本管理
	}
}

 dao层:

public class studentDao {
   private static final String TAG = "student";
   private Helper helper;//帮助类
   private SQLiteDatabase db; //数据库对象
   public studentDao(Context context){
	   helper=new Helper(context);
   }
   /*
    * 添加新的学员信息 
    */
   public boolean add(Student student){
	   try {
		   open();
		   db.execSQL("insert into t_student (name,age) values (?,?)",new Object[]{
				   student.getName(),student.getAge()
		   });
	       Log.i(TAG, "add success");
		   return true;
		
         } catch (Exception e) {
  	       Log.i(TAG, "add fail");
		     return false;
	        }
	   finally{
		  close();
  	       Log.i(TAG, "add close");
	   }
   }  
   /*
    * 更新学员信息
    * */
   public Boolean update(Student student){
	   try {
		   open();
		   db.execSQL("update t_student set name=?,age=? where sid=?",new Object[]{
				   student.getName(), student.getAge(), student.getSid()	   
		   });
		   Log.i(TAG, "update success!");
		   return true;
		
	} catch (Exception e) {
		   Log.i(TAG, "update fail!");
		return false;
	}
	   finally{
		   close();
	   }	  
   }  
   public Boolean del(int id){
	   try {
		 open();  
		db.execSQL("DELETE from t_student where sid="+id);
		Log.i(TAG, "删除成功");
		return true;
	} catch (Exception e) {
		Log.i(TAG, "删除失败");
		return false;
	}
	   finally{
		   close();
	   }
   }
   /*
    * 查找某个学员的信息
    * */
   public Student find(int id){
	   db=helper.getWritableDatabase();
	   Cursor cursor=db.rawQuery("select sid,name,age from t_student where sid=?", new String[]{String.valueOf(id)});
	   if(cursor.moveToNext()){
		   return new Student(cursor.getInt(cursor.getColumnIndex("sid")),cursor.getString(cursor.getColumnIndex("name")),cursor.getInt(cursor.getColumnIndex("short")));
	   }
	   return null;
   }
   /*
    * 打开数据库
    * */
   private void open(){
		db=helper.getReadableDatabase();
   }
   /*
    * 关闭
    * */
   private void close(){
	   helper.close();
   }
} 

  activity处理

  public class SQLiteActivity extends Activity {

   private EditText etName;
   private EditText etAge;
   private Button btnSubmit;
   model.Student student=new model.Student();
   studentDao sdo=new studentDao(SQLiteActivity.this);//调用数据库操作方法

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       etName=(EditText)findViewById(R.id.etName);
       etAge=(EditText)findViewById(R.id.etAge);
       btnSubmit=(Button)findViewById(R.id.btnSubmit);
       btnSubmit.setOnClickListener(listener);//点击添加数据
       add();
       update();
       del();
       
    }
    private OnClickListener listener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			add();
		}
	};
	//添加
	private void add(){
		student.setName(etName.getText().toString());
		   student.setAge(
				   Integer.valueOf(etAge.getText().toString()).intValue());
		   sdo.add(student);
	}
    //更新
	private void update(){
		  student.setName("测试数据");
	       student.setAge(120);
	       student.setSid(1);
	       sdo.update(student);
	}
	//删除
	private void del(){
		sdo.del(1);
	}

   xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名" />
    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年纪" />
    <EditText
        android:id="@+id/etAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加" />
</LinearLayout>

查询出来后怎么加载到相应的控件中,正在学习中

另外字符串类型转换成int类型时的正确方法:

 Integer.valueOf(string).intValue());

猜你喜欢

转载自284772894.iteye.com/blog/1733379