Android SqlLite操作不传Context 的骚操作

每当在Android 开发对sqllite 进行操作的时候总需要传一个Context 对象
接下来您目睹的这一切将结束这一切
快来看看我的蛇皮走位

第一步,你需要写一个Application的工具类
上代码

import android.app.Application;
import android.content.Context;


public class BaseApplication extends Application {

    private static Context mContext;


    public void onCreate() {

        super.onCreate();

        mContext = getApplicationContext();

    }


    public static Context getContext(){
        return mContext;

    }
}

写完 BaseApplication 后 要在 AndroidManifest.xml 的application 标签中进行绑定

android:name=".BaseApplication"

然后就是我们之前文章熟悉的Sqllite 操作了

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

public class OrderDBHelper extends SQLiteOpenHelper {
    private static final int DB_VERSION = 1;//数据库版本
    private static final String DB_NAME = "demo.db";//数据库名
    public static final String CODE= "code";//用户表名


    public OrderDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        try{
            String sql = "CREATE TABLE IF NOT EXISTS "+CODE+" (code text)";

            sqLiteDatabase.execSQL(sql);
        }catch (Exception e) {
            Log.d("创建表异常", String.valueOf(e));
            e.printStackTrace();

        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + CODE;
        onCreate(sqLiteDatabase);
    }
}

再写一个工具对数据库进行操作

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class OrderDao {

    public static void insertCode( String code){
        try{

            OrderDBHelper orderDBHelper = new OrderDBHelper(BaseApplication.getContext());
            SQLiteDatabase db = orderDBHelper.getReadableDatabase();
            String sqldel = "delete from "+ OrderDBHelper.CODE;
            db.execSQL(sqldel);

            String sql =  "insert into "+ OrderDBHelper.CODE+" values('"+code+"')";

            db.execSQL(sql);
            db.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public static String getCode(){
        String name = "";
        try{
            OrderDBHelper orderDBHelper = new OrderDBHelper(BaseApplication.getContext());
            SQLiteDatabase db = orderDBHelper.getReadableDatabase();
            String sql =  "select * from "+ OrderDBHelper.CODE;
            Cursor cursor = db.rawQuery(sql, new String[0]);
            if(cursor != null){
                cursor.moveToNext();
                name = cursor.getString(cursor.getColumnIndex("code"));
            }

            db.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return name;
    }
}

然后在你需要的地方去修改就好了
我只谢了查和插入,删和改的方式和插入一样
我是在工具类里做的BaseApplication,你也可以根据自己喜欢
然后你就尽情的发挥吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值