加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

sqlite

发布时间:2020-12-12 23:41:50 所属栏目:百科 来源:网络整理
导读:public class BulkingDBHelper extends SQLiteOpenHelper{ public static final String DATABASE_NAME = "bulkinfo.db" ; public static final int DATABASE_VERSION = 1 ; public static final String TABLE_NAME_SETUP = "setupinfo" ; //安装进度是否完成
public class BulkingDBHelper  extends SQLiteOpenHelper{
    public static final String DATABASE_NAME = "bulkinfo.db";
    public static final int DATABASE_VERSION= 1;
    public static final String  TABLE_NAME_SETUP="setupinfo";//安装进度是否完成  public static final String  TABLE_NAME_DOWNTYPE="downType";//分省下载选择类别记录  public static final String  TABLE_NAME_DOWNSTATUS="downLoadingStatus";//增量更新是否完成  public static final String  TABLE_NAME_BULKINGLIST="bulkList";//增量城市列表  public static final String  _ID="_id";
    public static final String  _NAME="_name";
    public static final String  _PROGRESS="_progress";
    public static final String  _DOWNTYPE="_downType";

    public BulkingDBHelper(Context con){
        super(con,DATABASE_NAME,null,DATABASE_VERSION);
    };
    public void onCreate(SQLiteDatabase db)//创建表  {
        String sql = "create table if not exists "+TABLE_NAME_SETUP +"(" +_ID +" Integer primary key,"  + _NAME + " text," + _PROGRESS + " Integer)";
        String sql2 = "create table if not exists "+TABLE_NAME_DOWNTYPE +"(" +_ID +" Integer primary key," + _DOWNTYPE + " Integer)";
        String sql3 = "create table if not exists "+TABLE_NAME_DOWNSTATUS +"(" +_ID +" Integer primary key," + _PROGRESS + " Integer)";
        String sql4 = "create table if not exists "+TABLE_NAME_BULKINGLIST +"(" +_ID +" Integer primary key," + _PROGRESS + " Integer)";
        db.execSQL(sql);
        db.execSQL(sql2);
        db.execSQL(sql3);
        db.execSQL(sql4);
    }



    public Cursor query() {
        SQLiteDatabase db=getWritableDatabase();//获得SQLiteDatabase实例  Cursor cursor=db.query(TABLE_NAME_SETUP,null);//查询获得Cursor  while(cursor.moveToNext())
        {
            String strValue= cursor.getString(0);
            String name=cursor.getString(1);
            Log.i("xxxxx","xxx strValue="+strValue+",name="+name);

        }
        return cursor;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
        // TODO Auto-generated method stub  }

}

--------------------------------------

public class DbManger {
    private String TAG = "DbManger";
    private Context context;
    public BulkingDBHelper helper;
    private Map<String,String> mSetupList = new HashMap<String,String>();
    private boolean hasDialog = false;
    public static final int DOWN_TYPE = 0;//分省下载的选择类别,比如3d  public static final int SETUP_TYPE = 1;//安装的进度是否完成  public static final int DOWNLOADING_STATUSE = 2;//下载完成个数  public static final int BULKING_LIST = 3;//下载完成个数  private ContentValues values=new ContentValues();
    private Cursor cursor =null;
    public DbManger(Context context){
        this.context = context;
        helper = getInstance(context);
    }

    public void dbCreate (){     //创建  Log.i(TAG,"----dbCreate");
        SQLiteDatabase db =  helper.getWritableDatabase();
    }

    public void dbInsert (ContentValues values,int type){
        SQLiteDatabase db = helper.getWritableDatabase();
        Log.i("xxxxx","xxxxx dbInsert values="+values.toString()+",type="+type);
        if(type == DOWN_TYPE){
            String sql = "create table if not exists "+BulkingDBHelper.TABLE_NAME_DOWNTYPE +"(" +BulkingDBHelper._ID +" Integer primary key,"  + BulkingDBHelper._NAME + " text," + BulkingDBHelper._DOWNTYPE + " Integer)";
            db.execSQL(sql);
            db.insert(BulkingDBHelper.TABLE_NAME_DOWNTYPE,values);
        }
        else if(type == SETUP_TYPE){
            String sql = "create table if not exists "+BulkingDBHelper.TABLE_NAME_SETUP +"(" +BulkingDBHelper._ID +" Integer primary key," + BulkingDBHelper._PROGRESS + " Integer)";
            db.execSQL(sql);
            db.insert(BulkingDBHelper.TABLE_NAME_SETUP,values);
        }
        else if(type == DOWNLOADING_STATUSE){
            String sql = "create table if not exists "+BulkingDBHelper.TABLE_NAME_DOWNSTATUS +"(" +BulkingDBHelper._ID +" Integer primary key," + BulkingDBHelper._PROGRESS + " Integer)";
            db.execSQL(sql);
            db.insert(BulkingDBHelper.TABLE_NAME_DOWNSTATUS,values);
        }
        else if(type == BULKING_LIST){
            String sql = "create table if not exists "+BulkingDBHelper.TABLE_NAME_BULKINGLIST +"(" +BulkingDBHelper._ID +" Integer primary key," + BulkingDBHelper._PROGRESS + " Integer)";
            db.execSQL(sql);
            db.insert(BulkingDBHelper.TABLE_NAME_BULKINGLIST,values);
        }
        db.close();
    }
    public int dbUpdate (ContentValues values,int code,int type){
        Log.i(TAG,"----dbUpdate");
        int ret = 0;
        if(dbQuerybyCode(code,type) == 1){
            return 1;
        }
        SQLiteDatabase db = helper.getWritableDatabase();
        String whereClause = BulkingDBHelper._ID+"=?";
        String[] whereArgs = new String[] { String.valueOf(code) };
        if(type == DOWNLOADING_STATUSE){
            db.update(BulkingDBHelper.TABLE_NAME_DOWNSTATUS,values,whereClause,whereArgs);
        }
        else  db.update(BulkingDBHelper.TABLE_NAME_SETUP,whereArgs);
       /* String updateSql = "update "+ BulkingDBHelper.TABLE_NAME + " set " + BulkingDBHelper._NAME +"='hangzhou' where " +BulkingDBHelper._ID + "=1";  DbManger.execSQL(db,updateSql);*/  db.close();
        return ret;
    }
    public int dbDelete (int code,"----dbDelete code="+code+",type="+type);
        if(helper == null){
            Log.i(TAG,"----dbDelete helper == null");
            //helper = getInstance(MainApplication.Instance().getApplicationContext());  }
        SQLiteDatabase db = helper.getWritableDatabase();

        String[] args = {String.valueOf(code)};
        int ret = 111;
        if(type == DOWN_TYPE){
            ret = db.delete(BulkingDBHelper.TABLE_NAME_DOWNTYPE,"_ID=?",args);
        } else if(type == SETUP_TYPE){
            ret = db.delete(BulkingDBHelper.TABLE_NAME_SETUP,args);
        }else if(type == BULKING_LIST){
            ret = db.delete(BulkingDBHelper.TABLE_NAME_BULKINGLIST,args);
        }
        Log.i("dbDelete","dbDelete ret="+ret);
        db.close();
        return ret;
    }
    public void dbDropTable (String tableName){     //删除表  Log.i(TAG,"----dbDropTable");

        SQLiteDatabase db =  helper.getWritableDatabase();
        String sql = "DROP TABLE IF EXISTS "+tableName;
        execSQL(db,sql);
    }
    public void dbClear (int type){
        Log.i(TAG,"----dbClear type="+type);
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "DELETE FROM " +BulkingDBHelper.TABLE_NAME_SETUP +";";
        execSQL(db,sql);
        String sql3 = "update sqlite_sequence set seq=0 where name ='setupinfo'";
        execSQL(db,sql3);
        db.close();
    }
    public int dbQuery(int type){
        int ret = 0;
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = null;
        if(type == DbManger.DOWNLOADING_STATUSE){
            cursor=db.query(BulkingDBHelper.TABLE_NAME_DOWNSTATUS,null);//查询获得Cursor   try {
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    int _id = cursor.getInt(0);
                    String _name=cursor.getString(1);
                    String _progress=cursor.getString(2);
                    if(_progress.equals("0"))
                        ret = 1;

                    Log.i("xxxx","xxxxxxd _id/_name/_progress="+_id+"/"+_name+"/"+_progress);
                }
            } finally {

            }
        }
        else{
            cursor=db.query(BulkingDBHelper.TABLE_NAME_SETUP,null);//查询获得Cursor  if(cursor.moveToFirst() ){//遍历游标  for(int i=0;i<cursor.getCount();i++){
                    cursor.move(i);
                    int _id = cursor.getInt(0);
                    String _name=cursor.getString(1);
                    String _progress=cursor.getString(2);
                    mSetupList.put("code"+_id,_id+"");
                    if(!_progress.equals("100"))
                        ret = 1;
                    Log.i("xxxx","xxxxxxs _id/_name/_progress="+_id+"/"+_name+"/"+_progress);
                }
            }
        }

        db.close();
        return ret;
    }

    public int dbQuerybyCode(int code,int type){
        int ret = 0;
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = null;
        String tableName = "";
        if(type == DbManger.DOWNLOADING_STATUSE){
            tableName = BulkingDBHelper.TABLE_NAME_DOWNSTATUS;
        } else if(type == DbManger.BULKING_LIST){
            tableName = BulkingDBHelper.TABLE_NAME_BULKINGLIST;
        }
        cursor=db.query(tableName,null);//查询获得Cursor  if(type == DbManger.DOWNLOADING_STATUSE || type == DbManger.BULKING_LIST){
            try {
                for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                    int _id = cursor.getInt(0);
                    String _name=cursor.getString(1);
                    String _progress=cursor.getString(2);
                    if(_id == code){
                        ret = 2;//已经在下载的进程中或者在增量城市列表   if(_progress.equals("100")){//下载完成  ret = 1;
                        }
                        Log.i("xxxx","xxxxxxd cunzai ---"+"/"+tableName);
                        break;
                    }
                    Log.i("xxxx","xxxxxxd _id/_name/_progress/tableName="+_id+"/"+_name+"/"+_progress+"/"+tableName);

                }
            } finally {}
        } else{
            cursor=db.query(BulkingDBHelper.TABLE_NAME_SETUP,null);//查询获得Cursor  if(cursor.moveToFirst() ){//遍历游标  for(int i=0;i<cursor.getCount();i++){
                    cursor.move(i);
                    int _id = cursor.getInt(0);
                    String _name=cursor.getString(1);
                    String _progress=cursor.getString(2);
                    if(_id == code){
                        ret = 2;//已经在安装的进程中  if(_progress.equals("100")){
                            ret = 1;
                        }
                        break;
                    }
                }
            }
        }

        db.close();
        return ret;
    }

    public Map<String,String> getCodeList (){
        return mSetupList;
    }

    public BulkingDBHelper getInstance (Context context){
        if(helper == null)
            helper = new BulkingDBHelper(context);
        return helper;
    }

    public static void execSQL (SQLiteDatabase db,String sql){
        if(db !=null){
            if(sql !=null && !"".equals(sql)){
                db.execSQL(sql);
            }
        }
    }
    public Cursor getCursor(String tableName){
        SQLiteDatabase db = helper.getWritableDatabase();
        cursor = db.query(tableName,null);//查询获得Cursor  return cursor;
    }
    public ContentValues setvalues ( String cityName,String progress ){
        values = new ContentValues();
        values.put(BulkingDBHelper._NAME,cityName);
        values.put(BulkingDBHelper._ID,code);
        values.put(BulkingDBHelper._PROGRESS,"0");
        return values;
    }

}
------------------------------------------
private DbManger dbManger ;
dbManger.dbCreate();
dbManger.dbInsert(values,DbManger.BULKING_LIST);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读