SQLite数据库简单封装和使用

一、简单封装一下

在这里插入图片描述

二、新建DBOpenHelper类

package com.example.myapplication.mysqlite3;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.myapplication.tool.MyConfig;

public class DBOpenHelper extends SQLiteOpenHelper {

    private static final String TAG = "DBOpenHelper";

    final String CREATE_TABLE_SQL1="create table "+MyConfig.table_gas8detectorwarm + "("
            +"id integer primary key autoincrement,"
            +"device_name  text,"
            +"topic_warm  text,"
            +"air  text,"
            +"temp_mim  text,"
            +"temp_max  text,"
            +"humi_min  text,"
            +"humi_max  text,"
            +"pm2_5  text,"
            +"pm10  text,"
            +"pm1_0  text,"
            +"CO2  text,"
            +"CH2O  text,"
            +"VOC  text)"
            ;
    final String CREATE_TABLE_SQL2="create table "+MyConfig.table_gas8detectorlog + "("
            +"id integer primary key autoincrement,"
            +"device_name  text,"
            +"topic  text,"
            +"logdata  text,"
            +"ctime  text)"
            ;

    //final String CREATE_TABLE_SQL = "create table testTable(id integer primary key autoincrement , data , date)";

    SQLiteDatabase sqLiteDatabase;

    public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, null, version); //重写构造方法并设置工厂为null
        Log.d(TAG, "DBOpenHelper: 创建数据库");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "onCreate: 创建数据表1"+CREATE_TABLE_SQL1);
        db.execSQL(CREATE_TABLE_SQL1); //创建单词信息表
        Log.d(TAG, "onCreate: 创建数据表2"+CREATE_TABLE_SQL2);
        db.execSQL(CREATE_TABLE_SQL2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //提示版本更新并输出旧版本信息与新版本信息

        String tring_sql1="drop table if exists "+MyConfig.table_gas8detectorwarm;
        db.execSQL(tring_sql1);

        String string_sql2="drop table if exists "+MyConfig.table_gas8detectorlog;
        db.execSQL(string_sql2);
        onCreate(db);
        Log.d(TAG, "onUpgrade: 数据库版本更新到 V"+newVersion);
    }
}

三、新建MySqlite类

package com.example.myapplication.mysqlite3;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.myapplication.tool.MyApplication;
import com.example.myapplication.tool.MyConfig;

public class MySqlite  {
    final static String TAG="MySqlite";
    public static MySqlite MySqlite1;
    static DBOpenHelper dbOpenHelper;  //定义DBOpenHelper,用于与数据库连接
    //String tableName="xiaozhoucun_table1";

    private MySqlite(){ }

    public  static MySqlite getInstance(){
        if (MySqlite1 == null) {
            MySqlite1 = new MySqlite();
            MySqliteCreate(MyApplication.getContext(), MyConfig.MySQLiteOpenHelper_DB_Name,null,MyConfig.MySQLiteOpenHelper_DB_version);
        }
        return MySqlite1;
    }


    public static void MySqliteCreate(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        dbOpenHelper = new DBOpenHelper(context, name, null, version);
    }


//********************************八合一********************************///

    //增
    public void gas8detectorwarm_insert(String device_name,String topic_warm,String air,String temp_mim,String temp_max,
                                        String humi_min,String humi_max,String pm2_5,String pm10,
                                        String pm1_0,String CO2,String CH2O,String VOC){
        ContentValues values = new ContentValues();

        values.put("device_name",device_name);
        values.put("topic_warm",topic_warm);
        values.put("air",air);
        values.put("temp_mim",temp_mim);
        values.put("temp_max",temp_max);
        values.put("humi_min",humi_min);
        values.put("humi_max",humi_max);
        values.put("pm2_5",pm2_5);
        values.put("pm10",pm10);
        values.put("pm1_0",pm1_0);
        values.put("CO2",CO2);
        values.put("CH2O",CH2O);
        values.put("VOC",VOC);

        dbOpenHelper.getWritableDatabase().insert(MyConfig.table_gas8detectorwarm,null,values);
    }

    //删
    public void gas8detectorwarm_delete(String device_name){
        dbOpenHelper.getWritableDatabase().delete(MyConfig.table_gas8detectorwarm,"device_name == ?",new String[]{device_name});
    }

//查
    public int gas8detectorwarm_query(String device_name){
        int i=0;

        Cursor cursor= dbOpenHelper.getWritableDatabase().query(MyConfig.table_gas8detectorwarm,null,"device_name = ?",new String[]{device_name},null,null,null);

        if (cursor.moveToFirst()){
            i= 1;
            do{
                int temp_id=cursor.getInt(cursor.getColumnIndex("id"));
                Log.d(TAG, "gas8detectorwarm_query: temp_id="+temp_id);
                String temp_device_name = cursor.getString(cursor.getColumnIndex("device_name"));
                Log.d(TAG, "gas8detectorwarm_query: temp_device_name="+temp_device_name);
                String temp_topic_warm = cursor.getString(cursor.getColumnIndex("topic_warm"));
                Log.d(TAG, "gas8detectorwarm_query: temp_topic_warm="+temp_topic_warm);
                String temp_air = cursor.getString(cursor.getColumnIndex("air"));
                Log.d(TAG, "gas8detectorwarm_query: temp_air="+temp_air);
                String temp_temp_mim = cursor.getString(cursor.getColumnIndex("temp_mim"));
                Log.d(TAG, "gas8detectorwarm_query: temp_temp_mim="+temp_temp_mim);
                String temp_temp_max = cursor.getString(cursor.getColumnIndex("temp_max"));
                Log.d(TAG, "gas8detectorwarm_query: temp_temp_max="+temp_temp_max);
                String temp_humi_min = cursor.getString(cursor.getColumnIndex("humi_min"));
                Log.d(TAG, "gas8detectorwarm_query: temp_humi_min="+temp_humi_min);
                String temp_humi_max = cursor.getString(cursor.getColumnIndex("humi_max"));
                Log.d(TAG, "gas8detectorwarm_query: temp_humi_max="+temp_humi_max);
                String temp_pm2_5 = cursor.getString(cursor.getColumnIndex("pm2_5"));
                Log.d(TAG, "gas8detectorwarm_query: temp_pm2_5="+temp_pm2_5);
                String temp_pm10 = cursor.getString(cursor.getColumnIndex("pm10"));
                Log.d(TAG, "gas8detectorwarm_query: temp_pm10="+temp_pm10);
                String temp_pm1_0 = cursor.getString(cursor.getColumnIndex("pm1_0"));
                Log.d(TAG, "gas8detectorwarm_query: temp_pm1_0="+temp_pm1_0);
                String temp_CO2 = cursor.getString(cursor.getColumnIndex("CO2"));
                Log.d(TAG, "gas8detectorwarm_query: temp_CO2="+temp_CO2);
                String temp_CH2O = cursor.getString(cursor.getColumnIndex("CH2O"));
                Log.d(TAG, "gas8detectorwarm_query: temp_CH2O="+temp_CH2O);
                String temp_VOC = cursor.getString(cursor.getColumnIndex("VOC"));
                Log.d(TAG, "gas8detectorwarm_query: temp_VOC="+temp_VOC);

            }while(cursor.moveToNext());
        }else{
            i=0;
        }

        return i;
    }




    //增
//    public static void insertData(String data) {
//        ContentValues values=new ContentValues();
//        MyCalendar myCalendar=new MyCalendar();
//        values.put("data", data);       //保存单词
//        values.put("date", myCalendar.getCalendar());  //保存解释
//        Log.d(TAG,"增  " + data  + myCalendar.getCalendar() );
//        dbOpenHelper.getWritableDatabase().insert(DBOpenHelper.tableName,null , values);
//    }
//    //删  此处没用到
//    public static void deleteData(int id) {
//        Log.d(TAG,"删" + String.valueOf(id ));
//        dbOpenHelper.getWritableDatabase().delete(DBOpenHelper.tableName, "id=?",new String[]{String.valueOf(id )});
//    }
//    //改  此处没用到
//    public static void updateData(String t_where) {
//        ContentValues values=new ContentValues();
//        values.put("from_where", t_where);  //更改来自茂名的
//        Log.d(TAG,"改 把黄光明改成来自" + t_where );
//        dbOpenHelper.getWritableDatabase().update(DBOpenHelper.tableName,values ,"name=?",new String[]{"黄光明"});
//    }
//    //查   此处没用到
//    public static void queryData(String t_where) {
//        Log.d(TAG,"查  来自哪里的所有数据 " + t_where );
//        //Cursor cursor=dbOpenHelper.getWritableDatabase().query("mymr", new String[]{"from_where"},"name==?",new String[]{"李世民"},null,null,null,null);
//        Cursor cursor=dbOpenHelper.getWritableDatabase().query(DBOpenHelper.tableName, null,"from_where=?", new String[]{"茂名"},null,null,null,null);
//        if(cursor.moveToFirst()){
//            Log.d(TAG,"cursor.moveToFirst()  true");
//        }
//        StringBuilder stringBuilder=new StringBuilder();
//        ArrayList arrayList=new ArrayList();
//        do{
//            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("name"));
//            stringBuilder.append(name);
//            stringBuilder.append("--");
//            @SuppressLint("Range") String from_where=cursor.getString(cursor.getColumnIndex("from_where"));
//            stringBuilder.append(from_where);
//            stringBuilder.append("\r\n");
//        }while(cursor.moveToNext());
//
//        Log.d(TAG,stringBuilder.toString());
//    }
//
//    //查
//    public static void queryAllData() {
//        Log.d(TAG,"查  全部数据" );
//        Cursor cursor=dbOpenHelper.getWritableDatabase().query(DBOpenHelper.tableName, null,null,null,null,null,null,null);
//        if(cursor.moveToFirst()){
//            Log.d(TAG,"cursor.moveToFirst()  true");
//        }
//
//        StringBuilder stringBuilder=new StringBuilder();
//        ArrayList arrayList=new ArrayList();
//        do{
//            @SuppressLint("Range") String data=cursor.getString(cursor.getColumnIndex("data"));
//            stringBuilder.append(data);
//            stringBuilder.append("--");
//            @SuppressLint("Range") String date=cursor.getString(cursor.getColumnIndex("date"));
//            stringBuilder.append(date);
//            stringBuilder.append("\r\n");
//        }while(cursor.moveToNext());
//
//        Log.d(TAG,stringBuilder.toString());
//    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值