Android学习中关于SQLite的一个小Demo(数据库的创建、数据的增删查改)

最近学习Android,做了一些小的东西,一直没有时间做个总结。经常总结对于学习新东西是很好的,说以今天整理一下自己做的东西,希望这样也有助于其他人学习。

我做的是一个基于Android平台,使用SQLite构建数据库,并且创建表来存储数据,还会涉及到数据的增删查改。整体的效果图如下:

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

上面就是Demo在调试时的几幅图,下面将会一一介绍这个Demo中用到的一些核心技术。

首先就是用到了SQLiteOpenHelper这个类,通过创建一个自己的帮助类来继承这个抽象类,来实现简单的对数据库创建和升级。SQLiteOpenHelper中有两个抽象方法,分别是onCreate()和onUpgrade(),我们必须在自己的帮助类里面重写这两个方法,然后分别在这两个方法中去实现创建、升级数据库的逻辑。SQLiteOpenHelper中有两个非常重要的实例方法,getReadableDatabase()和getWritableDatabase()。这两个方法都可以创建和打开一个现有的数据库(如果数据库已经存在则直接打开,否则创建一个新的数据库),并返回一个可对数据库进行读写操作的对象。不同的是,当数据库不可写入的时候(如磁盘空间已满)getReadabelDatabase()方法返回的对象将以只读的方式去打开数据库,而getWritableDatabase()方法则会出现异常。

MyDatabaseHelper.java
public class MyDatabaseHelper extends SQLiteOpenHelper {
   

    private static final String CREATE_TABLE = "create table peopleinfo (_id integer primary key autoincrement,name text not null,age integer,height float);";
    private Context mContext;

    public MyDatabaseHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists peopleinfo");
        onCreate(db);
    }

}

“drop table if exists peopleinfo”这句代码的作用就是当发现数据库中已经存在peopleinfo表,就将这两个表删除掉 ,然后调用onCreate()方法去重新创建。

构建People用来存储输入的People信息,方便向数据库中存储。

People.java
public class People {

    public int ID;
    public String Name;
    public int Age;
    public float Height;

    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int age) {
        Age = age;
    }

    public float getHeight() {
        return Height;
    }

    public void setHeight(float height) {
        Height = height;
    }

    @Override
    public String toString(){
        String result = "";
        result += "ID:" + this.ID + ",";
        result += "姓名:" + this.Name + ",";
        result += "年龄:" + this.Age + ", ";
        result += "身高:" + this.Height ;
        return result;
    }
}
activity_main.xml:主界面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="23sp"/>
    <EditText 
        android:id="@+id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄:"
            android:textSize="23sp"/>
        <EditText 
            android:id="@+id/edt_age"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numeric="integer"
            android:hint="请输入年龄"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高:"
            android:textSize="23sp"/>
        <EditText 
            android:id="@+id/edt_height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numeric=
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值