数据库

xml界面

主界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.shujukuapplication.MainActivity">
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20sp"
    android:id="@+id/xm"
    />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加一条新记录"
        android:id="@+id/tj"
        android:onClick="onClick"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除第一条记录"
        android:id="@+id/sc"
        android:onClick="onClick"
        />

</LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view"
        >


    </ListView>
</LinearLayout>

Adapter

自定义

public class Adapter extends CursorAdapter {


    public Adapter(MainActivity context, Cursor cursor) {
        super(context, cursor,0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.item_student,parent,false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView name=(TextView)view.findViewById(R.id.xm);
        name.setText(cursor.getString(cursor.getColumnIndex("name")));
    }
}

Helper

创建数据库


public class Helper extends SQLiteOpenHelper {
    private String sql="create table student1(_id integer primary key autoincrement," + "name text not null)";//sql语句
    public Helper(Context context){
        super(context,"student1Info", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql);//执行
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists student1");//如果存在表student1就删除
        onCreate(db);//重新创建
    }
}

Student

public class Student {
    private int _id;
    private String name;
 public  Student(){
 }
    public Student(String name) {
        this.name = name;
    }
    public int get_Id() {
        return _id;
    }
    public void set_Id(int _id) {
        this._id = _id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

StudentDAO

执行删除与添加语句

public class StudentDAO {
    private Context context;
    private Helper helper;
    private SQLiteDatabase db;
    public StudentDAO(Context context){
        helper=new Helper(context);
    }
    public void insert(Student student){
        db=helper.getWritableDatabase();
        String sql = "insert into student1(name)"+"values(?)";
        db.execSQL(sql, new Object[]{
              student.getName()}
        );
    }

    public  Cursor selectAll() {
        db = helper.getReadableDatabase();
        Cursor cursor = db.query("student1", null, null, null, null, null, null);
        return cursor;
    }
    public void delete(int id){
        String sql = "delete from student1 where _id=?";
        db.execSQL(sql, new Object[]{id});
    }
}

MainActivity

控件的获取

      xm=(EditText)findViewById(R.id.xm);
      view=(ListView)findViewById(R.id.view);

添加按钮

 public void onClick(View v) {
        String name=xm.getText().toString();
            switch (v.getId()){
                case R.id.tj:
                    //调用StudentDAO的insert()方法插入数据库
                    studentDAO=new StudentDAO(this);
                    student=new Student(name);
                    studentDAO.insert(student);//插入
                    studentDAO =new  StudentDAO(this);
                    cursor=studentDAO.selectAll();
                    adapter = new Adapter(this,cursor);
                    view.setAdapter(adapter);//更新数据
                    break;

删除按钮

case R.id.sc:
                try {//try语句是因为假如没记录会闪退
                    cursor.moveToFirst();//数据的第一行
                    student=new Student();
                    student.set_Id(cursor.getInt(cursor.getColumnIndex("_id")));//获取数据第一行的id
                    studentDAO.delete(student.get_Id());
                    cursor=studentDAO.selectAll();
                    adapter = new Adapter(this, cursor);
                    view.setAdapter(adapter);
                   Toast.makeText(this,"删除成功",Toast.LENGTH_LONG).show();
                } catch (Exception e){
                    Toast.makeText(this,"记录为空",Toast.LENGTH_LONG).show();
                }
        break;
        ```

![](http://images2015.cnblogs.com/blog/1127051/201705/1127051-20170515145856510-1362190395.png)

![](http://images2015.cnblogs.com/blog/1127051/201705/1127051-20170515145913416-1741168491.png)

![](http://images2015.cnblogs.com/blog/1127051/201705/1127051-20170515145918416-1098729012.png)

![](http://images2015.cnblogs.com/blog/1127051/201705/1127051-20170515145922932-696906657.png)
posted @ 2017-05-15 14:59  皮皮皮·  阅读(566)  评论(0编辑  收藏  举报