记使用Android SQLite遇到的小问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Callon_H/article/details/51428341

引用请标明出处:http://blog.csdn.net/callon_h/article/details/51428341

之前一直是遇到activity需要保存数据就直接使用SQLite就好,但是有一天突然发现,我需要在其中一个Activity里访问另一个Activity建立的SQLiteDatabase,从而引出了该文。

查了很多资料,大多数都是说,使用单例模式建立继承SQLiteOpenHelper的类,然后使用静态方法(一般取名getInstance)来获得。自己亲自尝试了,发现总是不对,于是找了一种更简便更懒的方法(直接用文件位置引用数据库):
SettingActivity.java

db = openOrCreateDatabase("callon_set_pump", Context.MODE_PRIVATE, null);
        if(tabbleIsExist("table1")){
            DatabaseHelper dbHelper = new DatabaseHelper(SettingActivity.this, "callon_set_pump");
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            Cursor cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                //add your code here
            }
            cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"2"}, null, null, null);
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                //add your code here
            }
            cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"3"}, null, null, null);
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                //add your code here
            }
            cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"4"}, null, null, null);
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                //add your code here
            }
            cursor = db.query("table1", new String[]{"id","name"}, "id=?", new String[]{"5"}, null, null, null);
            while(cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
               //add your code here
            }

        }
        else {
            db.execSQL("CREATE TABLE table1(id int,name varchar(20))");
            ContentValues values = new ContentValues();
            values.put("id", 1);
            values.put("name", "5000");
            db.insert("table1", null, values);
            values = new ContentValues();
            values.put("id", 2);
            values.put("name", "1000");
            db.insert("table1", null, values);
            values = new ContentValues();
            values.put("id", 3);
            values.put("name", "3");
            db.insert("table1", null, values);
            values = new ContentValues();
            values.put("id", 4);
            values.put("name", "3");
            db.insert("table1", null, values);
            values = new ContentValues();
            values.put("id", 5);
            values.put("name", "NONE");
            db.insert("table1", null, values);
            //add your code here
        }

上述类建立了一个数据库callon_set_pump,在另一个类中使用的话,
PageActivity.java

db_set = SQLiteDatabase.openOrCreateDatabase("data/data/com.autopump.callon.nenu/databases/callon_set_pump",null);
        Cursor cursor = db_set.query("table1", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            System.out.println("name is :"+ name);
        }

关于tabbleIsExist方法,它是用来检测是否存在该数据库的,如果存在重复创建会出现异常的哦~

private boolean tabbleIsExist(String tableName){
        boolean result = false;
        if(tableName == null){
            return false;
        }
        SQLiteDatabase db = null;
        DatabaseHelper dbHelper = new DatabaseHelper(SettingActivity.this, "callon_set_pump");
        Cursor cursor = null;
        try {
            db = dbHelper.getReadableDatabase();
            String sql = "select count(*) as c from Sqlite_master  where type ='table' and name ='"+tableName.trim()+"' ";
            cursor = db.rawQuery(sql, null);
            if(cursor.moveToNext()){
                int count = cursor.getInt(0);
                if(count>0){
                    result = true;
                }
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
        return result;
    }

总之能抓到耗子的就是好猫吧,虽然方法不像什么单例模式那样洋气,希望对大家有帮助。

猜你喜欢

转载自blog.csdn.net/Callon_H/article/details/51428341