How to organize a database access code in an Android project?

I created a ContentProvider for my main Sqlite table, pretty much following the NotePad Example from the SDK (although I'm not sure if I will ever expose my data to other applications). However, I need to create many other non-trivial queries for this and other tables and views. A good example would be queries to extract some statistics from baseline data, averages, totals, etc.

So what is the best place for this code in an Android project? How should this be related to and related to Uri-based data access provided by the Provider? Are there any good examples?

+3
source share
2 answers

From the point of view of keepabillty, I believe that the provider model is the cleanest way to abstract the data access code. And from the experience of working on a large application, in the end the application will grow to such an extent that some data must be presented through the model of the provider (for example, the introduction of services in the application). However, exposing a lot of different representations of your data can be a lot of work in the provider model.

If you went on a route, I would carefully think about how you display data via URLs, and, as a rule, with some complexity you are talking about, you can manage using subdirectories representing different representations of the data (similar to REST )

, DA . SQLiteOpenHelper DA ( ), DA. Android . , MediaProvider.java, , , . , , , . DbSSLSessionCache.java

+3

, - . , , .

, / . / , .

DB.

: ( ) , , :

public class VoyagerDB extends SQLiteOpenHelper {
    @Override
    public void onCreate(SQLiteDatabase db) {
            boolean ret = false;

            // build out the schema
            ret = populateSchema(db);

    }


    /**
     * Returns information from a given obdRequest record.
     * @param requestID
     * @return
     */
    public Cursor getRequestInfo (String requestID) {

            Cursor c = null;

            String sql = "SELECT id _id, active,request,formula, description,frequency,minValue,maxValue,numDataBytes " +
                            "FROM obdRequest " +
                            "WHERE ID=" + requestID;

            c = localDBRW.rawQuery(sql, null);

            return c;
    }


    /**
     * If the given settings key exists in the DB, return its record ID. Otherwise return blank.
     * @param key
     * @return
     */
    public String settingsKeyExists (String key) {
            String recordID = "";
            Cursor c = null;

            String sql = "SELECT id,key,value from settings WHERE key = ?";
            String selectionArgs[] = {key};
            c = localDBRW.rawQuery(sql, selectionArgs);

            if (c == null) {
                    return "";
            }
            if (c.getCount()<1) {
                    c.close();
                    return "";
            }

            c.moveToFirst();
            recordID = c.getString(c.getColumnIndex("id"));
            c.close();

            return recordID;
    }

}
+1

Source: https://habr.com/ru/post/1745806/


All Articles