SQL

这次的作业是要实现数据库的添加与删除

首先是XML布局

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_add"
            android:text="添加"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/btn_update"
            android:text="修改"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/btn_delete"
            android:text="删除"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>
    </LinearLayout>
View Code

然后是JavaActivity

public class NameDAO {
private Context context;//上下文
private MyDBHelper helper;//创建,升级,打开数据库
private SQLiteDatabase db;//对表增删改查

public NameDAO(Context context){
    helper=new MyDBHelper(context);
}
public void insert(Name name){
    //打开数据库
    db=helper.getWritableDatabase();
    //生成数据集合
    ContentValues values=new ContentValues();
    values.put("name",name.getName());
    //执行语句
    db.insert("student",null,values);
}

public Cursor selectAll() {
    db =helper.getReadableDatabase();
    Cursor cursor= db.query("student",null,null,null,null,null,null);
    return cursor;
}
public void delete(int id){
    db = helper.getWritableDatabase();
    db.delete("student","_id=?",new String[]{String.valueOf(id)});//根据数据的id号来删除数据
}
View Code

没了

posted on 2017-05-16 14:58  王千小弟  阅读(98)  评论(0)    收藏  举报