数据库
首页 > 数据库> > Android之SQLite

Android之SQLite

作者:互联网

一、介绍

        SQLite是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite是一个增长最快的数据库引擎,这是在普及方面的增长,与它的尺寸大小无关。SQLite 源代码不受版权限制。

二、使用

        1、创建

        首先SqliteHelper继承SQLiteOpenHelper重写onCreate与onUpgrade方法。其中onCreate是用来创建数据库表的,onUpgrade是用来更新数据库表与数据库表字段,SqliteHelper方法需传入数据库名称与数据库版本。实例代码如下:

public interface DBStructure {

    // 数据库名称
    public static final String DB_NAME = "temperature.db";
    // 数据库版本
    public static final int DB_VERSION = 1;

    //表名称
    public static final String SURFACE_NAME_OPEN_DOOR = "openDoor";
    //表字段
    public static final String OPEN_DOOR_RECORD =
            "CREATE TABLE " + SURFACE_NAME_OPEN_DOOR + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                    "TYPE TEXT," +
                    "IDINFO TEXT," +
                    "QRTYPE TEXT," +
                    "QRINFO TEXT," +
                    "NUCLEATEINFO TEXT," +
                    "VACCINATIONINFO TEXT," +
                    "STATUS TEXT," +
                    "OPERATETIME Long," +
                    "IMAGE TEXT," +
                    "VIDEO TEXT," +
                    "TEMPERATURE TEXT)";

    public static final String SURFACE_NAME_FACE_FEATURES = "faceFeatures";
    public static final String FACE_FEATURES_MD5 = "CREATE TABLE faceFeatures (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "featuresName TEXT," +
            "featuresMD5 TEXT)";

    public static final String SURFACE_NAME_CARD = "card";
    public static final String CARD_CARDNO = "CREATE TABLE card (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "RID int," +
            "CARDNO Long)";

}
    public SqliteHelper(Context context) {
        super(context, DBStructure.DB_NAME, null, DBStructure.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(DBStructure.OPEN_DOOR_RECORD);
        if (DBStructure.DB_VERSION == 2) {
            sqLiteDatabase.execSQL(DBStructure.CARD_CARDNO);
        } else if (DBStructure.DB_VERSION == 4) {
            sqLiteDatabase.execSQL(DBStructure.FACE_FEATURES_MD5);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (DBStructure.DB_VERSION == 2 && newVersion > oldVersion) {
            db.execSQL(DBStructure.CARD_CARDNO);
        } else if (DBStructure.DB_VERSION == 3 && newVersion > oldVersion) {
            db.execSQL("ALTER TABLE openDoor ADD VACCINATIONINFO TEXT;");
        } else if (DBStructure.DB_VERSION == 4 && newVersion > oldVersion) {
            db.execSQL(DBStructure.FACE_FEATURES_MD5);
        }
    }

        2、CRUD

使用Android系统提供的增删改查,代码如下:

public class OpenDoorSqlistHelper {

    private SqliteHelper mSqliteHelper;

    public OpenDoorSqlistHelper(SqliteHelper mSqliteHelper) {
        this.mSqliteHelper = mSqliteHelper;
    }

    /**
     * 新增
     * @param TYPE
     * @param IDINFO
     * @param QRTYPE
     * @param QRINFO
     * @param NUCLEATEINFO
     * @param VACCINATIONINFO
     * @param STATUS
     * @param OPERATETIME
     * @param IMAGE
     * @param VIDEO
     * @param TEMPERATURE
     */
    public void add(String TYPE, String IDINFO, String QRTYPE, String QRINFO, String NUCLEATEINFO,
                    String VACCINATIONINFO, String STATUS, long OPERATETIME, String IMAGE, String VIDEO,
                    String TEMPERATURE) {
        SQLiteDatabase writableDatabase = mSqliteHelper.getWritableDatabase();
        ContentValues mContentValues = new ContentValues();
        mContentValues.put("TYPE", TYPE);
        mContentValues.put("IDINFO", IDINFO);
        mContentValues.put("QRTYPE", QRTYPE);
        mContentValues.put("QRINFO", QRINFO);
        mContentValues.put("NUCLEATEINFO", NUCLEATEINFO);
        mContentValues.put("VACCINATIONINFO", VACCINATIONINFO);
        mContentValues.put("STATUS", STATUS);
        mContentValues.put("OPERATETIME", OPERATETIME);
        mContentValues.put("IMAGE", IMAGE);
        mContentValues.put("VIDEO", VIDEO);
        mContentValues.put("TEMPERATURE", TEMPERATURE);
        writableDatabase.insert(DBStructure.SURFACE_NAME_OPEN_DOOR, null, mContentValues);
    }

    /**
     * 查询所有
     * @return
     */
    public Cursor findAll() {
        return mSqliteHelper.getReadableDatabase().query(DBStructure.SURFACE_NAME_OPEN_DOOR, null, null, null, null, null, null);
    }

    /**
     * 条件查询
     *
     * @param columns       需要查询字段
     * @param selection     条件语句
     * @param selectionArgs 占位符内容
     * @return
     */
    public Cursor findCondition(String[] columns, String selection, String[] selectionArgs) {
        return mSqliteHelper.getReadableDatabase().query(DBStructure.SURFACE_NAME_OPEN_DOOR, columns, selection, selectionArgs, null, null, null);
    }

    /**
     * 修改
     * @param values      需要修改的字段
     * @param whereClause 修改条件语句
     * @param whereArgs   占位符内容
     */
    public void update(ContentValues values, String whereClause, String[] whereArgs) {
        mSqliteHelper.getWritableDatabase().update(DBStructure.SURFACE_NAME_OPEN_DOOR, values, whereClause, whereArgs);
    }

    /**
     * 条件删除
     *
     * @param whereClause 条件语句
     * @param whereArgs   占位符内容
     */
    public void delete(String whereClause, String[] whereArgs) {
        mSqliteHelper.getWritableDatabase().delete(DBStructure.SURFACE_NAME_OPEN_DOOR, whereClause, whereArgs);
    }

    /**
     * 删除所有
     */
    public void deleteAll() {
        mSqliteHelper.getWritableDatabase().delete(DBStructure.SURFACE_NAME_OPEN_DOOR, null, null);
    }

}

新增

insert的构造方法

public long insert (String table, String nullColumnHack, ContentValues values)  

table

要插入数据的表的名称 

values

ContentValues对象,类似一个map通过键值对的形式存储值。

nullColumnHack

当values参数为空或者里面没有内容的时候,insert是会失败的(底层数据库不允许插入一个空行),为了防止这种情况,要在这里指定一个列名,到时候如果发现将要插入的行为空行时,就会将你指定的这个列名的值设为null,然后再向数据库中插入。通过观察源码的insertWithOnConflict方法可以看到当ContentValues类型的数据initialValues为null或size<=0时,就会在sql语句中添加nullColumnHack的设置。

删除

delete的构造方法

public int delete(String table, String whereClause, String[] whereArgs)

table

要插入数据的表的名称 

whereClause

删除条件,可以为null。

whereArgs

占位符,可以为null。

修改

update的构造方法

public int update(String table, ContentValues values, String whereClause, String[] whereArgs)

table

要插入数据的表的名称 

values

ContentValues对象,类似一个map通过键值对的形式存储值。

whereClause

修改条件。

whereArgs

占位符。

查询

query的构造方法

public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

table

要插入数据的表的名称 。

columns

要查询的列名,可以是多个,可以为null,表示查询所有列。

selection

查询条件,比如id=? and name=? 可以为null。

selectionArgs

对查询条件赋值,一个问号对应一个值,按顺序 可以为null。

having

语法have,可以为null。

orderBy

语法,按xx排序,可以为null。

        3、调用

调用没有什么好说的主要是对应传参即可,代码如下:

public class MainActivity extends AppCompatActivity {

    static String TAG = "MainActivity";
    SqliteHelper mSqliteHelper;
    OpenDoorSqlistHelper mOpenDoorSqlistHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSqliteHelper = new SqliteHelper(this);
        mOpenDoorSqlistHelper = new OpenDoorSqlistHelper(mSqliteHelper);
        mSqliteHelper.getWritableDatabase().beginTransaction();  //开启事件
        try {
            mOpenDoorSqlistHelper.add("1", "11111", "11111", "11111", "11111", "11111", "T", System.currentTimeMillis(), "11111", "11111", "11111");
            mOpenDoorSqlistHelper.add("2", "2222222", "2222222", "2222222", "2222222", "2222222", "T", System.currentTimeMillis(), "2222222", "2222222", "2222222");
            Cursor condition = mOpenDoorSqlistHelper.findCondition(new String[]{"IDINFO", "QRTYPE", "QRINFO", "OPERATETIME"}, "TYPE=?", new String[]{"1"});
            if (condition.moveToFirst()) {
                do {
                    String idinfo = condition.getString(condition.getColumnIndex("IDINFO"));
                    String qrtype = condition.getString(condition.getColumnIndex("QRTYPE"));
                    String qrinfo = condition.getString(condition.getColumnIndex("QRINFO"));
                    long operatetime = condition.getLong(condition.getColumnIndex("OPERATETIME"));
                    Log.d(TAG, "onCreate: 条件查询 idinfo: " + idinfo + "\tqrtype: " + qrtype + "\tqrinfo: " + qrinfo + "\toperatetime: " + operatetime);
                } while (condition.moveToNext());
            }
            condition.close();
            ContentValues mContentValues = new ContentValues();
            mContentValues.put("IDINFO", "321");
            mContentValues.put("QRTYPE", "321");
            mContentValues.put("QRINFO", "321");
            mContentValues.put("STATUS", "F");
            mContentValues.put("OPERATETIME", System.currentTimeMillis());
            mOpenDoorSqlistHelper.update(mContentValues, "TYPE=?", new String[]{"1"});
            Cursor all = mOpenDoorSqlistHelper.findAll();
            if (all.moveToFirst()) {
                do {
                    String idinfo = all.getString(all.getColumnIndex("IDINFO"));
                    String qrtype = all.getString(all.getColumnIndex("QRTYPE"));
                    String qrinfo = all.getString(all.getColumnIndex("QRINFO"));
                    all.getString(all.getColumnIndex("NUCLEATEINFO"));
                    all.getString(all.getColumnIndex("VACCINATIONINFO"));
                    String status = all.getString(all.getColumnIndex("STATUS"));
                    all.getString(all.getColumnIndex("IMAGE"));
                    all.getString(all.getColumnIndex("VIDEO"));
                    all.getString(all.getColumnIndex("TEMPERATURE"));
                    long operatetime = all.getLong(all.getColumnIndex("OPERATETIME"));
                    Log.d(TAG, "onCreate: 查询所有 idinfo: " + idinfo + "\tqrtype: " + qrtype + "\tqrinfo: " + qrinfo + "\tstatus: " + status+"\toperatetime: " + operatetime);
                } while (all.moveToNext());
            }
            all.close();
            mOpenDoorSqlistHelper.delete("TYPE=?",new String[]{"1"});
            Cursor all1 = mOpenDoorSqlistHelper.findAll();
            if (all1.moveToFirst()) {
                do {
                    String idinfo = all1.getString(all1.getColumnIndex("IDINFO"));
                    String qrtype = all1.getString(all1.getColumnIndex("QRTYPE"));
                    String qrinfo = all1.getString(all1.getColumnIndex("QRINFO"));
                    all1.getString(all1.getColumnIndex("NUCLEATEINFO"));
                    all1.getString(all1.getColumnIndex("VACCINATIONINFO"));
                    String status = all1.getString(all1.getColumnIndex("STATUS"));
                    all1.getString(all1.getColumnIndex("IMAGE"));
                    all1.getString(all1.getColumnIndex("VIDEO"));
                    all1.getString(all1.getColumnIndex("TEMPERATURE"));
                    long operatetime = all1.getLong(all1.getColumnIndex("OPERATETIME"));
                    Log.d(TAG, "onCreate: 查询所有 all1 idinfo: " + idinfo + "\tqrtype: " + qrtype + "\tqrinfo: " + qrinfo + "\tstatus: " + status+"\toperatetime: " + operatetime);
                } while (all1.moveToNext());
            }
            all1.close();
            mOpenDoorSqlistHelper.deleteAll();
        } catch (Exception e) {
        } finally {
            mSqliteHelper.getWritableDatabase().endTransaction(); //结束事件
        }
    }
}

需要注意的是查询的时候返回的游标处理,以及游标关闭,同时操作数据库CRUD的时候需要开启事件,结束的时候关闭事件,分别是函数beginTransaction()开启事件,endTransaction()关闭事件。

补充

上述所有的是的Android API所支持的数据库CRUD,若是sql语句掌握很好更建议使用sql语句进行数据库操作,使用更灵活。

以上仅限于个人理解操作,若是有更好方法可以留言分享。

标签:SQLite,String,getString,getColumnIndex,all1,Android,null,public
来源: https://blog.csdn.net/duanchuanzhi/article/details/120503242