如何创建一个Android数据库Demo?

Android数据库Demo

如何创建一个Android数据库Demo?

在Android开发中,SQLite数据库是一个常用的本地存储解决方案,本文将通过一个简单的Demo演示如何在Android应用中使用SQLite数据库进行增删改查(CRUD)操作,这个Demo包括以下几个部分:创建实体类、建立数据表操作类、实现增删改查功能以及在Activity中调用这些功能。

一、新建实体类

我们需要定义一个实体类来表示数据表中的记录,以下是一个简单的Person类示例:

/**
 * 实体类
 */
public class Person {
    // 数据库自增ID
    public int id;
    // 用户名
    public String name;
    // 发送时间
    public long time;
    // 发送消息
    public String msg;
    public Person() {}
    public Person(String n, long t, String m) {
        name = n;
        time = t;
        msg = m;
    }
    @NonNull
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("[");
        builder.append(id).append("--");
        builder.append(name).append("--");
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        builder.append(format.format(date)).append("--");
        builder.append(msg).append("]");
        return builder.toString();
    }
}

二、建立数据表操作类

我们需要创建一个数据库操作类,用于管理数据表的创建和数据的增删改查操作,以下是一个简单的DBDao类示例:

如何创建一个Android数据库Demo?

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteException;
/**
 * 数据库操作类
 */
public class DBDao {
    private static final String DB_NAME = "per.db";
    private static final String TABLE_NAME = "person_info";
    private static final int DB_VERSION = 1;
    private SQLiteDatabase mDatabase;
    private Context mContext;
    private DBOpenHelper mDbOpenHelper;
    public DBDao(Context context) {
        mContext = context;
    }
    public void openDataBase() {
        mDbOpenHelper = new DBOpenHelper(mContext, DB_NAME, null, DB_VERSION);
        try {
            mDatabase = mDbOpenHelper.getWritableDatabase(); //获取可写数据库
        } catch (SQLException e) {
            mDatabase = mDbOpenHelper.getReadableDatabase(); //获取只读数据库
        }
    }
    public void closeDataBase() {
        if (mDatabase != null) {
            mDatabase.close();
        }
    }
    public long insertData(Person per) {
        ContentValues values = new ContentValues();
        values.put("name", per.name);
        values.put("time", per.time);
        values.put("msg", per.msg);
        return mDatabase.insert(TABLE_NAME, null, values);
    }
    public boolean deleteData(long id) {
        return mDatabase.delete(TABLE_NAME, "id=" + id, null) > 0;
    }
    public boolean updateData(Person per) {
        ContentValues values = new ContentValues();
        values.put("name", per.name);
        values.put("time", per.time);
        values.put("msg", per.msg);
        return mDatabase.update(TABLE_NAME, values, "id=" + per.id, null) > 0;
    }
    public Cursor queryAllData() {
        return mDatabase.query(TABLE_NAME, null, null, null, null, null, null);
    }
}

三、建立数据库帮助类

为了管理数据库的创建和版本升级,我们需要创建一个继承自SQLiteOpenHelper的数据库帮助类:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * 数据库帮助类,必须继承自 SQLiteOpenHelper
 */
public class DBOpenHelper extends SQLiteOpenHelper {
    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table person_info(id integer primary key autoincrement, name varchar(20), time long, msg varchar(200))");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("alter table person_info add account varchar(20)");
    }
}

四、在Activity中调用数据库操作方法

我们在Activity中实例化DBDao类,并调用其方法来执行增删改查操作:

如何创建一个Android数据库Demo?

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends Activity {
    private DBDao dbDao;
    private TextView textView;
    private Person person;
    private long currentTime;
    private SimpleDateFormat simpleDateFormat;
    private String[] infos;
    private StringBuilder stringBuilder;
    private Cursor cursor;
    private String[] columns;
    private String[] datas;
    private String str;
    private String[] splits;
    private int i;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;
    private TextView textView6;
    private TextView textView7;
    private TextView textView8;
    private TextView textView9;
    private TextView textView10;
    private TextView textView11;
    private TextView textView12;
    private TextView textView13;
    private TextView textView14;
    private TextView textView15;
    private TextView textView16;
    private TextView textView17;
    private TextView textView18;
    private TextView textView19;
    private TextView textView20;
    private TextView textView21;
    private TextView textView22;
    private TextView textView23;
    private TextView textView24;
    private TextView textView25;
    private TextView textView26;
    private TextView textView27;
    private TextView textView28;
    private TextView textView29;
    private TextView textView30;
    private TextView textView31;
    private TextView textView32;
    private TextView textView33;
    private TextView textView34;
    private TextView textView35;
    private TextView textView36;
    private TextView textView37;
    private TextView textView38;
    private TextView textView39;
    private TextView textView40;
    private TextView textView41;
    private TextView textView42;
    private TextView textView43;
    private TextView textView44;
    private TextView textView45;
    private TextView textView46;
    private TextView textView47;
    private TextView textView48;
    private TextView textView49;
    private TextView textView50;
    private TextView textView51;
    private TextView textView52;
    private TextView textView53;
    private TextView textView54;
    private TextView textView55;
    private TextView textView56;
    private TextView textView57;
    private TextView textView58;
    private TextView textView59;
    private TextView textView60;
    private TextView textView61;
    private TextView textView62;
    private TextView textView63;
    private TextView textView64;
    private TextView textView65;
    private TextView textView66;
    private TextView textView67;
    private TextView textView68;
    private TextView textView69;
    private TextView textView70;
    private TextView textView71;
    private TextView textView72;
    private TextView textView73;
    private TextView textView74;
    private TextView textView75;
    private TextView textView76;
    private TextView textView77;
    private TextView textView78;
    private TextView textView79;
    private TextView textView80;
    private TextView textView81;
    private TextView textView82;
    private TextView textView83;
    private TextView textView84;
    private TextView textInfo1;
    private TextView textInfo2;
    private TextView textInfo3;
    private TextView textInfo4;
    private TextView textInfo5;
    private TextView textInfo6;
    private TextView textInfo7;
    private TextView textInfo8;
    private TextView textInfo9;
    private TextView textInfo10;
    private TextView textInfo11;
    private TextView textInfo12;
    private TextView textInfo13;
    private TextView textInfo14;
    private TextView textInfo15;
    private TextView textInfo16;
    private TextView textInfo17;
    private TextView textInfo18;
    private TextView textInfo19;
    private TextView textInfo20;
    private TextView textInfo21;
    private TextView textInfo22;
    private TextView textInfo23;
    private TextView textInfo24;
    private TextView textInfo25;
    private TextView textInfo26;
    private TextView textInfo27;
    private TextView textInfo28;
    private TextView textInfo29;
    private TextView textInfo30;
    private TextView textInfo31;
    private TextView textInfo32;
    private TextView textInfo33;
    private TextView textInfo34;
    private TextView textInfo35;
    private TextView textInfo36;
    private TextView textInfo37;
    private TextView textInfo38;
    private TextView textInfo39;
    private TextView textInfo40;
    private TextView textInfo41;
    private TextView textInfo42;
    private TextView textInfo43;
    private TextView textInfo44;
    private TextView textInfo45;
    private TextView textInfo46;
    private TextView textInfo47;
    private TextView textInfo48;
    private TextView textInfo49;
    private TextView textInfo50;
    private TextView textInfo51;
    private TextView textInfo52;
    private TextView textInfo53;
    private TextView textInfo54;
    private TextView textInfo55;
    private TextView textInfo56;
    private TextView textInfo57;
    private TextView textInfo58;
    private TextView textInfo59;
    private TextView textInfo60;
    private TextView textInfo61;
    private TextView textInfo62;
    private TextView textInfo63;
    private TextView textInfo64;
    private TextView textInfo65;
    private TextView textInfo66;
    private TextView textInfo67;
    private TextView textInfo68;
    private TextView textInfo69;
    private TextView textInfo70;
    private TextView textInfo71;
    private TextView textInfo72;
    private TextView textInfo73;
    private TextView textInfo74;
    private TextView textInfo75;
    private TextView textInfo76;
    private TextView textInfo77;
    private TextView textInfo78;
    private TextView textInfo79;
    private TextView textInfo80;
    private TextView textInfo81;
    private TextView textInfo82;
    private TextView textInfo83;
    private TextView textInfo84;
    private TextView textInfo85;
    private TextView textInfo86;
    private TextView textInfo87;
    private TextView textInfo88;
    private TextView textInfo89;
    private TextView textInfo90;
    private TextView textInfo91;
    private TextView textInfo92;
    private TextView textInfo93;
    private TextView textInfo94;
    private TextView textInfo95;
    private TextView textInfo96;
    private TextView textInfo97;
    private TextView textInfo98;
    private TextView textInfo99;
    private TextView textInfo100;
    private TextView textInfo101;
    private TextView textInfo102;
    private TextView textInfo103;
    private TextView textInfo104;
    private TextView textInfo105;
    private TextView textInfo106;
    private TextView textInfo107;
    private TextView textInfo108;
    private TextView textInfo109;
    private TextView textInfo110;
    private TextView textInfo111;
    private TextView textInfo112;

以上内容就是解答有关“android数据库demo”的详细内容了,我相信这篇文章可以为您解决一些疑惑,有任何问题欢迎留言反馈,谢谢阅读。

原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/628754.html

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seo的头像K-seoSEO优化员
Previous 2024-11-06 02:16
Next 2024-11-06 02:26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入