Get an OBD facility



  • There's an ObjectA in it there's an ObjectB field and there are two tables A and B respectively. How am I supposed to get ObjectB. I'll just say I don't want to use OrmLite, Realm, etc. Only SQLite is a wardrobe. So I want to know if there's any right and beautiful solution to this task?

    public class ObjectA {
    

    private int id;
    private ObjectB objB;

    ObjectA(Cursor cursor) {
    id = CursorUtils.getInt("id", cursor);
    objB = ?
    по идее тут должен получить ID ObjectB который будет соотв. записи в таблице B. После чего получить запись и создать из неё объект.
    }

    }

    public class ObjectB {

    private int id;
    private String descr;

    }

    The base looks as follows.
    Table A:
    id integer
    objB integer

    Table B:
    id integer
    decr text



  • To solve your question (to get an object from SQLite) the best solution is the solution in the B.Hardy Android book. 290-page version of the CursorWrapper.

    The point is that the srapper creates a dial between the cadet and the requesting class, which allows for the use of " nautical " SQLite " tools on the one hand, and on the other hand, receives an object rather than a raw sample of the cadet.

    It looks like that.

    You're in class.

    public class SomeCursorWrapper extends CursorWrapper {
        public SomeCursorWrapper(Cursor cursor) {
            super(cursor);
        }
    
    public ObectA getObject() {
        String title = getString(getColumnIndex(Database.TITLE));
        long date = getLong(getColumnIndex(Database.DATE));
    
        ObjectA obj = new ObectA();
        obj.setTitle(title);
        obj.setDate(new Date(date));
        return obj;
    }
    

    }

    Using, for example, an ID facility:

    public ObjectA getObectA(long id) {

    Cursor с = Database.query(
            "someTable",
            null, 
            "_id = ?",
            new String[]{Long.toString(id)},
            null, 
            null, 
            null 
    );
    SomeCursorWrapper cursor = new SomeCursorWrapper (c);
    cursor.moveToFirst();
    return cursor.getObect();
    

    }

    Of course, not necessarily getting a cadet straight in the method, the rapper can use a previous cadet. The point of the class is that you give him the cadet and you get the object back.

    Because I couldn't find the equivalents on the Internet, and if you were interested in the solution, you'd need to find this book to get details.

    You can see all this in the case. https://github.com/amogh147/CriminalIntent - classes CrimeCursorWrapper and CrimeLab

    On the receipt of the facility as one of the fields of the table, given that the ObjB field contains an ID on the relevant line in table B, something such as the request should solve the problem.

    The request for rawQuery is preferable:

    SELECT * FROM A LEFT JOIN B ON A.ObjB = B.id

    The request for query (with no change for the problem method directly):

    SELECT * FROM A,B WHERE A.ObjB = B.id

    As a result of this sample, we will be able to obtain the recordings in the course, each row of which will contain table A columns and table B columns, with ID, matching the number stored in field ObjB of table A. To reduce problems in one of the tables id You need to change your name to something like that. id_B♪ Thus, each line received by such a request will have the following structure of the columns (with the proposed change of name):

    | id | ObjB | id_B | decr |

    If any columns are not required, they may be deleted by specifying in the request for the names of the columns of both tables that should be in the sample (e.g., delete ObjB, as it does not provide more useful information:

    SELECT A.id, B.id_B, B.decr FROM A LEFT JOIN B ON A.ObjB = B.id

    If not all recordings are required, but only one with a defined ID from Table A (click here with ID = 1, any value can be added to the numerical location through the code variable):

    SELECT A.id, B.id_B, B.decr FROM A,B WHERE A.id = 1 AND A.ObjB = B.id

    Only retrieval of the data and distribution of the relevant facilities remains in the srapper. Since you didn't attach a specific code on the work with the OBD, I'm not gonna write an abstract example, I think you're in the power to use this information yourself.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2